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?

2
  • 2
    What did you try so far? Commented Sep 23, 2020 at 15:13
  • See the second answer from the above duplicate Commented Sep 23, 2020 at 15:15

3 Answers 3

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));


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

2 Comments

Using .some() instead of .find() would probably make more sense here (as filter expects a boolean to be returned, saving conversion)
aye, truthy has me spoiled
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));

7 Comments

using name a first name for comparison is fine for small things. but an ID is best when using filter on objects.
@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.
if the set is only unique values getting pulled by the name property. it will fail if there are duplicate first names.
No, it will not fail altruios, because the result would still have those duplicates.
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.
|
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)

2 Comments

using name a first name for comparison is fine for small things. but an ID is best when using filter on objects.
@altruios Agree with you, but we did not have Id in this particular scenario.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.