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?
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?
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))))
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));
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');