I have a nested array of objects, I'm trying to filter the given array of objects using a property from the third level of its array property value. For example, from the below array I like to filter the entire array using the property "state" = "NY".
organisation = [
{
"dept_id":1,
"dept":{
"name":"finance",
"employees":[
{
"emp_id":1,
"name":"John",
"address":[
{
"country":"US",
"state":"NC"
}
]
}
]
}
},
{
"dept_id":2,
"dept":{
"name":"marketing",
"employees":[
{
"emp_id":2,
"name":"David",
"address":[
{
"country":"US",
"state":"NY"
}
]
}
]
}
},
{
"dept_id":3,
"dept":{
"name":"sales",
"employees":[
{
"emp_id":3,
"name":"Robert",
"address":[
{
"country":"US",
"state":"NC"
}
]
}
]
}
}
]
As a result of using the filter "state":"NY" my expected answer is
organisation = [
{
"dept_id":2,
"dept":{
"name":"marketing",
"employees":[
{
"emp_id":2,
"name":"David",
"address":[
{
"country":"US",
"state":"NY"
}
]
}
]
}
}
]
How can I use filter method using javascript to get this expected result.
organisation.filter(o => o.dept.employees.some(e => e.address.some(a => a.state === 'NY')))