2 回答
TA贡献1789条经验 获得超10个赞
您可以过滤对象并排除它们。我已经注释掉了部分比较,因为不清楚是否要过滤这些属性以及如何过滤。你只提到了地点。如果您希望它包含所有属性的所有匹配结果,&&请将||.
如前所述,如果属性匹配(或具有一致的命名),则可以简化和概括代码。
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));
TA贡献1860条经验 获得超9个赞
如果您只想测试一个条件,只需使用该语句并丢弃其余条件,或者如果您想让任何一个条件为真以获得结果,则将 && 逻辑更改为 || 必要的陈述。
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]);
});
添加回答
举报