gpbaculio
gpbaculio

Reputation: 5968

How to check if objects in array have ID property present in the argument IDs?

I need help how to do this, I have todos and I want to update the todos isComplete property in the array if its ID is in the argument, something like this code:

const todos = [{id: 1, text:"one",isComplete:false},{id:2,text:"two",isComplete:false},{id:3,text:"three",isComplete:false}];

const myArgs = [2,3];
const completeAllTodos = todos.filter(todoItem.id =>  myArgs.includes(todoItem.id)); 
completeAllTodos.map( todoItem => todoItem.isComplete = true);

On completeAllTodos I want to return todos objects that have an ID present on the argument array then update completeAllTodos isComplete property to true. I also want to do it asynchronously but I am new in javascript. I've been thinking of how to do it for hours but I can't make my brain do what I want. help?

Upvotes: 0

Views: 57

Answers (3)

vol7ron
vol7ron

Reputation: 42099

Just do it all in the filter loop block:

const todos = [{
  id: 1,
  text: "one",
  isComplete: false
}, {
  id: 2,
  text: "two",
  isComplete: false
}, {
  id: 3,
  text: "three",
  isComplete: false
}];

const args = [2, 3];
const completedTodos = todos.filter(item => args.includes(item.id) && (item.isComplete=true));

console.log(completedTodos);

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138247

If you just wanna set isCompletes to true:

const todos = [{id: 1, text:"one",isComplete:false},{id:2,text:"two",isComplete:false},{id:3,text:"three",isComplete:false}];
myArgs = [2,3];
todos.forEach(el=>myArgs.indexOf(el.id)+1?el.isComplete=true:0);

If you want this elems too:

const todos = [{id: 1, text:"one",isComplete:false},{id:2,text:"two",isComplete:false},{id:3,text:"three",isComplete:false}];
myArgs = [2,3];
var result=todos.filter(el=>myArgs.indexOf(el.id)+1).map(el=>!(el.isComplete=true)||el);

And if you want both:

const todos = [{id: 1, text:"one",isComplete:false},{id:2,text:"two",isComplete:false},{id:3,text:"three",isComplete:false}];
myArgs = [2,3];
var results=todos.reduce((arr,el)=>myArgs.indexOf(el.id)+1?(el.isComplete=true,arr.push(el),arr):arr);

http://jsbin.com/wopexiwime/edit?console

If you really need an async implementation (i dont think so):

function forEach(arr,call,i=0){
   if(i>=arr.length){
      return;
   }
   call(arr[i],i);
   setTimeout(forEach,0,arr,call,i+1);
}

function filter(arr,call,finalcall,i=0,res=[]){
   if(i>=arr.length){
      return finalcall(res);
   }
   if(call(arr[i],i)) res.push(arr[i]);
   setTimeout(filter,0,arr,call,finalcall,i+1,res);
}

function map(arr,call,finalcall,i=0,res=[]){
   if(i>=arr.length){
      return finalcall(res);
   }
   res.push(call(arr[i],i));
   setTimeout(map,0,arr,call,finalcall,i+1,res);
}

map([1,2,3,4],(e,i)=>e+i,console.log);

Upvotes: 1

Phix
Phix

Reputation: 9880

Here's one way.

const todos = [{
  id: 1,
  text: "one",
  isComplete: false
}, {
  id: 2,
  text: "two",
  isComplete: false
}, {
  id: 3,
  text: "three",
  isComplete: false
}];

const myArgs = [2, 3];
const completeAllTodos = todos
  .filter(todo => {
    return myArgs.includes(todo.id)
  })
  .map(todo => {
    todo.isComplete = true
    return todo
  });
console.log(completeAllTodos)

Upvotes: 1

Related Questions