I want to filter an array of objects by items of another array. This is my list of items:
const list = ["first", "second", "third"]
This array can be empty, with one, two or three itens... By example:
const list = []
const list = ["first"]
let list = ["first", "third"]
So, I need to use my list to filter my myArr2 by status.
const myArr2 = [
{
id: "1",
language: "portuguese",
status: "first"
},
{
id: "2",
language: "portuguese",
status: "first"
},
{
id: "3",
language: "portuguese",
status: "second"
},
{
id: "4",
language: "portuguese",
status: "third"
},
{
id: "5",
language: "portuguese",
status: "second"
},
]
If my list is:
const list = [ "first", "third" ]
The output must be:
const myArr2 = [
{
id: "1",
language: "portuguese",
status: "first"
},
{
id: "2",
language: "portuguese",
status: "first"
},
{
id: "4",
language: "portuguese",
status: "third"
},
]
If my list is:
const list = [ "first" ]
The output must be:
const myArr2 = [
{
id: "1"
language: "portuguese"
status: "first"
},
{
id: "2"
language: "portuguese"
status: "first"
}
}
and so on...
I made this code but not works properly:
Object.entries(list).forEach(status => {
myArr2.filter(item => {
if(item.status === status){
return item
}
})
})
What am I doing wrong? Someone can help me?