0

I'm new to Javascript and I'm struggling with how to use the map, filter, find and other functions. I have two arrays of objects, and I wanted to filter the first one with the second.

const users = [ 
  { name: 'Anna', age: 22, gender: 'F' }, 
  { name: 'John', age: 25, gender: 'M' },
  { name: 'Mary', age: 27, gender: 'F' },
  { name: 'Joe',  age: 30, gender: 'M' } 
] 

const filter = [ 
  { name: 'Anna' }, 
  { name: 'John' } 
] 

// Here is the expected result:
const expected_result = [ 
  { name: 'Anna', age: 22, gender: 'F' },
  { name: 'John', age: 25, gender: 'M' } 
] 

Does anyone know what is the best way to do this?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Amanda
  • 41
  • 8

3 Answers3

1

something like this?

const filteredUsers = users.filter(user=>filter.find(x=>(x.name==user.name)&&(x.age==user.age)&&(x.gender==user.gender));


what would be better is to give everyone a unique id.

const filteredUsers = users.filter(user=>filter.find(x=>(x.id==user.id));


altruios
  • 986
  • 1
  • 10
  • 29
1

"What is the best way" is a matter of opinion, but if you would have a large filter object, then it makes sense to first convert it to a temporary set:

function filterBy(users, filter) {
    let set = new Set(filter.map(({name}) => name)); // for faster lookup
    return users.filter(({name}) => set.has(name));
}

// demo
const users = [{name: 'Anna',age: 22,gender: 'F',},{name: 'John',age: 25,gender: 'M',},{name: 'Mary',age: 27,gender: 'F',},{name: 'Joe',age: 30,gender: 'M',},];
const filter = [{name: 'Anna',},{name: 'John',}];
console.log(filterBy(users, filter));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • using name a first name for comparison is fine for small things. but an ID is best when using filter on objects. – altruios Sep 23 '20 at 15:17
  • @altruios, I suppose this comment if meant for the OP. No-one claimed that the thing to filter has to be unique... one can filter anything; it doesn't matter whether it is unique or not. – trincot Sep 23 '20 at 15:18
  • if the set is only unique values getting pulled by the name property. it will fail if there are duplicate first names. – altruios Sep 23 '20 at 15:19
  • 1
    No, it will not fail altruios, because the result would still have those duplicates. – trincot Sep 23 '20 at 15:19
  • new Set()... set is only unique items... const users = [{name: 'Anna',age: 22,gender: 'F',},{name: 'Anna',age: 43,gender: 'F',},{name: 'John',age: 25,gender: 'M',},{name: 'Mary',age: 27,gender: 'F',},{name: 'Joe',age: 30,gender: 'M',},]; the filter would fail here. if there is only 1 Anna in that set. which there would be because set returns a collection of unique items. – altruios Sep 23 '20 at 15:21
  • @altruios, you miss the point: the set is only a temporary hash. It is not the set that is returned. It is the result of the `.filter` call that is the result, and `.filter` returns *all* results that match with the set, also duplicates. Please test first. – trincot Sep 23 '20 at 15:22
  • ah, my mistake was thinking the filter was more than names... that it was unique people objects... which is not the case. – altruios Sep 23 '20 at 15:30
0

const users = [{
    name: 'Anna',
    age: 22,
    gender: 'F',
  },
  {
    name: 'John',
    age: 25,
    gender: 'M',
  },
  {
    name: 'Mary',
    age: 27,
    gender: 'F',
  },
  {
    name: 'Joe',
    age: 30,
    gender: 'M',
  },
];

const filter = [{
    name: 'Anna',
  },
  {
    name: 'John',
  },
];

const result = users.filter(value => {
  const isExist = filter.findIndex(data => data.name === value.name);
  return isExist === -1 ? false: true;
});

console.log(result)
Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28