score:6

Accepted answer

Your second array is not valid. You have to wrap the string values with quotes.

You can use Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

and Array.prototype.includes()

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

Try the following way:

var arr1 = [1, 3, 4,];

var arr2 = [
  {
    id: 1,
    title: 'Title 1',
   },
  {
    id: 2,
    title: 'Title 2',
   },
];

var res = arr2.filter(i => arr1.includes(i.id));

console.log(res);


Related Query

More Query from same tag