Here are two simple arrays, one is an original list, the other is a selected index from the original one.
I just want to re-create a new array, based on the selected index array.
Here is my code.
const list = [{
id: 0,
name: "Ken"
},
{
id: 1,
name: "Ryu"
},
{
id: 2,
name: "Sakura"
},
{
id: 3,
name: "Vega"
}
]
const index = [1, 3]
//I want to get a new array like this, making use of both of the arrays above:
filterdList = [
{
id: 1,
name: "Ryu"
},
{
id: 3,
name: "Vega"
}
]
I tried lots of things but I just messed up and was just stack.
How would you accomplish this?
Thank you in advance
>Solution :
You can use Array.prototype.filter
const list=[{id:0,name:"Ken"},{id:1,name:"Ryu"},{id:2,name:"Sakura"},{id:3,name:"Vega"},];
const index = [1, 3];
const filteredList = list.filter(({id}) => index.includes(id))
console.log(filteredList);