我有一个嵌套的对象数组,我试图从其数组属性值的第三层使用属性来过滤给定的对象数组。例如,从下面的数组中,我喜欢使用属性"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"
}
]
}
]
}
}
]
由于使用过滤器"state":"NY"
,我的预期答案是
organisation = [
{
"dept_id":2,
"dept":{
"name":"marketing",
"employees":[
{
"emp_id":2,
"name":"David",
"address":[
{
"country":"US",
"state":"NY"
}
]
}
]
}
}
]
我如何使用javascript的过滤器方法来获得这个预期的结果。
您可以使用Array.prototype.filter
,Array.prototype.some
和Array.prototype.find
函数来实现所需的结果。试试这个,
const 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"}]}]}}];
const filter = "NY";
const res = organisation.filter(
item => item.dept.employees.some(
employee => employee.address.find(address => address.state === filter)
)
);
console.log(res);
在这里,我将filter
应用于organization
数组,然后检查employees
数组中的任何员工是否具有state
值">NY的address
。"。
您可以使用Array.prototype.filter
和Array.prototype.some
的组合来过滤出您想要的结果。
const 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"}]}]}}];
const result = organisation.filter(
item => item.dept.employees.some(
employee => employee.address.some(address => address.state === 'NY') // Filter condition
)
);
console.log(result);
基本上,这过滤了organization
数组,其中一些empoloyee
有一些address
,state
值为'NY'
。
const 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"}]}]}}];
const filter = "NY";
const res = organisation.filter(item => item.dept.employees.find(add => JSON.stringify(add.address).includes('"state":"' + filter + '"')));
console.log(res);
你只可以使用过滤器
filtered = organisation.filter(el => el.dept.employees.filter(el => el.address.filter(el => el.state == 'NY').length > 0 ).length > 0);
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"
}
]
}
]
}
}
]
filtered = organisation.filter(el => el.dept.employees.filter(el => el.address.filter(el => el.state == 'NY').length > 0 ).length > 0);
console.log(filtered);
我这样过滤它。试试这个
const filteredOrg = [];
organisation.forEach((item) => {
item.dept.employees.forEach((employe) => {
employe.address.forEach((address) => {
if (address.state === "NY") {
filteredOrg.push(item);
}
});
});
});
我希望它对你也有用。
编码快乐!