我有一个对象,该对象具有三个不同的数组,如location Vertical和roundType,我将得到一个过滤器对象,该对象在该对象中具有相同的三个数组。这是需要过滤的数据
testObject = [{
"id": 1892928,
"vertical_tax": [
678,
664
],
"location_tax": [
666
],
"roundType": [
"rt1"
],
}
{
"id": 1892927,
"vertical_tax": [
662,
663
],
"location_tax": [
663
],
"roundType": [
"rt2"
],
}]
这是应该基于其进行过滤的过滤器对象
filterObject = {
locations: [666,667]
roundTypes: ["rt1","rt2"]
verticals: [662,661]
}
原始要求:要在任一过滤器对象数组中获取具有特定值的任何对象。这可以通过使用“ some”来完成。 更新要求:因此,我需要使用在filterObject中传递的值来过滤主对象。因此,filterObject中的所有条件都应匹配。应该返回所有匹配的ID。这可以通过“每个”来完成
答案 0 :(得分:2)
您可以过滤对象并仅排除它们。我已注释掉部分比较,因为尚不清楚是否要筛选这些属性以及筛选方式。您只提到了位置。如果希望它包括所有属性的所有匹配结果,请将&&
更改为||
。
如前所述,如果属性匹配(或具有一致的命名),则可以简化和泛化代码。
filterObjects
过滤存在的任何匹配项。
filterObjects1
要求存在verticals
中的所有元素,以及其他属性中的任何匹配项。
testObject = [{
"id": 1892928,
"vertical_tax": [
678,
664
],
"location_tax": [
666
],
"roundType": [
"rt1"
],
},
{
"id": 1892927,
"vertical_tax": [
662,
663
],
"location_tax": [
663
],
"roundType": [
"rt2"
],
}]
filterObject = {
locations: [666,667],
roundTypes: ["rt1","rt2"],
verticals: [662,661]
};
const filterObjects = (filterObject, testObject) => {
return testObject.filter(obj =>
obj.location_tax && obj.location_tax.some(
x => filterObject.locations && filterObject.locations.includes(x)) ||
obj.roundType && obj.roundType.some(
x => filterObject.roundTypes && filterObject.roundTypes.includes(x)) ||
obj.vertical_tax && obj.vertical_tax.some(
x => filterObject.verticals && filterObject.verticals.includes(x))
);
};
console.log(filterObjects(filterObject, testObject));
filterObject = {
roundTypes: ["rt1","rt2"],
verticals: [662,661]
};
console.log(filterObjects(filterObject, testObject));
// require presence of all objects in filterObject.verticals using .every
const filterObjects1 = (filterObject, testObject) => {
return testObject.filter(obj =>
(obj.location_tax && obj.location_tax.some(
x => filterObject.locations && filterObject.locations.includes(x)) ||
obj.roundType && obj.roundType.some(
x => filterObject.roundTypes && filterObject.roundTypes.includes(x))
) &&
filterObject.verticals.every( x => obj.vertical_tax && obj.vertical_tax.includes(x) )
);
};
filterObject = {
roundTypes: ["rt1","rt2"],
verticals: [662,663]
};
delete testObject[0].vertical_tax;
console.log(filterObjects1(filterObject, testObject));
// 必须匹配所有 filterObject属性中存在的某些值
testObject = [{
"id": 1892928,
"vertical_tax": [
678,
664
],
"location_tax": [
666
],
"roundType": [
"rt1"
],
},
{
"id": 1892927,
"vertical_tax": [
662,
663
],
"location_tax": [
663
],
"roundType": [
"rt2"
],
}]
filterObject = {
locations: [666,667],
roundTypes: ["rt1","rt2"],
// verticals: [662,661,678]
};
// _must_ match some value in _all_ filterObject properties _that exist_
const filterObjects = (filterObject, testObject) => {
return testObject.filter(obj =>
(!filterObject.locations || obj.location_tax && obj.location_tax.some(
x => filterObject.locations && filterObject.locations.includes(x))) &&
(!filterObject.roundTypes || obj.roundType && obj.roundType.some(
x => filterObject.roundTypes && filterObject.roundTypes.includes(x))) &&
(!filterObject.verticals || obj.vertical_tax && obj.vertical_tax.some(
x => filterObject.verticals && filterObject.verticals.includes(x)))
);
};
console.log(filterObjects(filterObject, testObject));
答案 1 :(得分:0)
如果只想测试一个条件,则只需使用该语句并丢弃其余条件,或者如果您希望使任一条件都为真以获取结果,则将&&逻辑更改为||。以获得必要的陈述。
testObject.filter( i => {
return i.vertical_tax.every((value, index) => value === filterObject.verticals[index]) &&
i.location_tax.every((value, index) => value === filterObject.locations[index]) &&
i.roundType.every((value, index) => value === filterObject.roundTypes[index]);
});