I have an array of objects that I need to filter based on their statuses.
const data = [
{
id:1,
name:"data1",
status: {
open:1,
closed:1,
hold:0,
block:1
}
},
{
id:2,
name:"data2",
status: {
open:1,
closed:0,
hold:4,
block:1
}
},
{
id:3,
name:"data3",
status: {
open:0,
closed:0,
hold:4,
block:0
}
}
]
The statuses are defined in an array like this:
const statuses = ['open','closed']
I want to filter all data that contains the status "open" and "closed" with a value greater than 0. The expected result should be:
const result = [
{
id:1,
name:"data1",
status: {
open:1,
closed:1,
hold:0,
block:1
}
}]
Here is my attempt at filtering the data:
const result = data.filter(item => {
return (
statuses.forEach(val => {
if (item.status[val] > 0)
return item
})
)
})
I'm sure I'm missing something in my code, so any help would be greatly appreciated. Thank you.