这是我从服务器得到的结果。
arrays=
[ 0:
{ Id: 1
, name: 'test1'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 1:
{ Id: 2
, name: 'test2'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'disk_init', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 2:
{ Id: 3
, name: 'test3'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 3:
{ Id: 4
, name: 'test4'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 5:
{ Id: 5
, name: 'test5'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial_disk_init', type: 'generic' }
, { name: 'application_role', value: 'disk_initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
]
}
]
我需要用多种条件来过滤它。
filters = {
application_name: 'initial',
application_role: 'disk_init_role'
}
我做了一个函数来过滤数据,但这只适用于一个条件
filterArray (array, filters) {
for (var [key, value] of Object.entries(filters)) {
const res = Object.values(array).filter(
item => item.parameters.some(param => param.name === `${key}` && param.value === `${value}`)
)
return results
}
}
在阵列中循环以满足两个contion(过滤器)的最佳方式是什么?结果只需要id 1和4,并且需要在返回结果数组中。
3条答案
按热度按时间czfnxgou1#
基本上,您正在查找中的每一项
arrays
每一项都符合你的要求filters
包含。为此,您可以使用
every
关于filters
并试图find
有那个的项目name
并匹配value
```const result = arrays.filter(item => {
return Object.entries(filters)
.every( ([key,value]) => item.parameters.find(p => p.name == key).value == value)
});
const arrays=
[
{ Id: 1
, name: 'test1'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 2
, name: 'test2'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'disk_init', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 3
, name: 'test3'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 4
, name: 'test4'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 5
, name: 'test5'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial_disk_init', type: 'generic' }
, { name: 'application_role', value: 'disk_initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
]
}
]
const filters = {
application_name: 'initial',
application_role: 'disk_init_role'
}
const result = arrays.filter(item => {
return Object.entries(filters).every( ([key,value]) => item.parameters.find(p => p.name == key).value == value)
});
console.log(result);
yr9zkbsy2#
这会将所有过滤器应用于每个数组元素。仅当满足所有筛选条件时,才会将数组元素添加到结果数组中。
ljo96ir53#
如果希望任何筛选条件匹配,请使用此代码
如果希望所有筛选条件都匹配,请使用此代码。