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' }
]
*/Code language: JavaScript (javascript)

More information

Leave a Reply

Your email address will not be published. Required fields are marked *