Filter out array from another array – JavaScript

Here's a small snippet to filter an array using another array with JavaScript.

const students = [
 { name: 'Name1', id: 123, other: 'tests' },
 { name: 'Name2', id: 124, other: 'tests' },
 { name: 'Name3', id: 125, other: 'tests' }
];

const competitors = [
 { name: 'Name1', id: 456, other: 'tests', student: 123 }
];

const result = students.filter((student) => 
  competitors.every((competitor) => competitor.student !== student.id));

console.log(result);

/*
Console Output: 

[
{ name: 'Name2', id: 124, other: 'tests' },
{ name: 'Name3', id: 125, other: 'tests' }
]
*/

More information

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*