0

I want to filter an array with another array in order to know if there are new people.

const people = [
    { "name": "jerry" },
    { "name": "tom" },
    { "name": "alex" }
]
const newList = [
    { "name": "bran" },
    { "name": "jerry" },
    { "name": "john" }
]

const new_people = []

for (const pp of people) {

    let result = newList.filter(newL => newL.name != pp.name)

    if (result) {
        new_people.push(result)
    }
}

console.log(new_people)

This is the result:

[
  [ { name: 'bran' }, { name: 'john' } ],
  [ { name: 'bran' }, { name: 'jerry' }, { name: 'john' } ],
  [ { name: 'bran' }, { name: 'jerry' }, { name: 'john' } ]
]

But I'm looking for:

[ { name: 'bran' }, { name: 'john' } ]

I would like to avoid the loop because it makes duplicate in the result but I don't know how I can't do it without the loop.

3 Answers 3

2

First make a temporary array of people name:

const peopleNames = people.map(pp => pp.name);

Then the peopleNames will be as follows:

['jerry', 'tom', 'alex']

Now filter the new people from the newList:

const newPeople = newList.filter(pp => !peopleNames.includes(pp.name));

The newPeople will be an array of objects that you are looking for.

[{name: 'bran'}, {name: 'john'}]
Sign up to request clarification or add additional context in comments.

Comments

1

const people = [
    { "name": "jerry" },
    { "name": "tom" },
    { "name": "alex" }
]
const newList = [
    { "name": "bran" },
    { "name": "jerry" },
    { "name": "john" }
]
const output = newList.filter(a => people.filter(x => x.name == a.name).length == 0);
console.log('By USing Filter', output);
//filter the newList and retain those object, which are not present in the people

//Way 2: By using some
//const output2 = newList.filter(a => !people.some(x => x.name == a.name));
//console.log('By Using Some', output2);

2 Comments

Thank you, way 2 is simple to understand. But for way 1, it is difficult to understand the following part: ".length == 0". I thought by removing ".length == 0" I would get [{ name: 'jerry' }] but I didn't
@GrowyTobe, If you want to use Way 1 and expect to get the output i.e. present in both [{name: 'jerry'}] you can replace .length == 0 with .length > 0
1

You can use Array's reduce method to get the desired result:

const new_people = newList.reduce((accVal, e) => 
    (people.map(p => p.name).includes(e.name)) 
    ? accVal  
    : accVal.concat({ "name": e.name } ), 
[ ] )

2 Comments

If I'm not wrong you use the conditional (ternary) operator in reduce method. Shouldn't accVal have the value of [{ name: 'jerry' }] as "jerry" is in both array?
Initially accVal will be an empty array ([ ]). So, when there is a match nothing happens. When there is no match the element from newList is added to the accVal. Hence the result will have the expected elements - [ { name: 'bran' }, { name: 'john' } ].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.