-1

i have an array:

arr 1 =
[
{id: 0, title: "A"},
{id: 1, title: "B"},
{id: 2, title: "C"},
]

And another array:

arr2 =
["0", "1"]

And from the arr1 I would like to choose object with id == arr2[i]

How to easily do this?

3 Answers3

1

const arr1 =[
{id: 0, title: "A"},
{id: 1, title: "B"},
{id: 2, title: "C"},
]
const arr2 =["0", "1"]

console.log(arr1.filter(({ id }) => arr2.includes(String(id))))
bel3atar
  • 913
  • 4
  • 6
0

This will get you all matches to ids in the arr2 array

const arr1 =[
{id: 0, title: "A"},
{id: 1, title: "B"},
{id: 2, title: "C"},
]
const arr2 =["0", "1"]

console.log(arr1.filter(el => arr2.indexOf(String(el.id)) != -1));
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

You can use the array's method find()

const arr1 =[
{id: 0, title: "A"},
{id: 1, title: "B"},
{id: 2, title: "C"},
]
const arr2 =["0", "1"]

const obj = arr1.find(e => e.id == arr2[1]);

console.log(obj || 'Not found');
anotherOne
  • 1,513
  • 11
  • 20