0

This is the issue Im trying to solve. I know how to do direct comparison with objects, but Im caught how to make an either or property comparison inside a filter fx.

// tasks are objs with {name: string,id: int}

const taskArray = [task1, task2, task3, task3];

// f(x) needs to take obj param and see if it matches either/or with 
// any task obj k/v pair
// my implementation so far, missing something important, help here


const filterTasks = (taskArray, obj) => {
  taskArray.filter( task => Object.keys(task)  // i know I have to do some equality opperator here but stuck

  return taskArray;
}
7
  • Can you please provide more information on what your filtering criteria is? Commented May 30, 2018 at 17:35
  • yes I want to filter some match based off the taskArr and match it with some key or values of the passed in obj Commented May 30, 2018 at 17:45
  • this is not a dup! Commented May 30, 2018 at 17:46
  • So, either the same name or the same id, or both, should all match, right? Commented May 30, 2018 at 17:46
  • ys Attersson I know how to match a basic obj Commented May 30, 2018 at 17:47

1 Answer 1

2

There it is. filterTasks will return an array that contains only tasks in the passed array that match at least 1 key/val pair in the passed object.

tArr = [{name: "hi",id: 1},{name: "hola",id: 2},{name: "hello",id: 3},{name: "bye",id: 4}];

const filterTasks = (taskArray, obj) => taskArray.filter( task => Object.keys(task).some( key => obj[key] && obj[key]==task[key]));

console.log(filterTasks(tArr,{name:"hi",id:2}));

Sign up to request clarification or add additional context in comments.

4 Comments

i always forget about the some() method thanks
that was just my browser crashing sorry
you are welcome
i cant seem to ask even a legit question without the moderator hounds downvoting me on this site. Im glad at least you gave me a chance