使用多个过滤器值过滤对象

我有一个对象,它具有三个不同的数组,例如位置垂直和圆形类型,我将获得一个过滤器对象,该对象将在该对象中具有相同的三个数组。这是需要过滤的数据


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]

   }

原始要求:是在任一过滤器对象数组中获取具有特定值的任何对象。这可以通过使用“一些”来完成。更新要求:所以我需要使用 filterObject 中传递的值来过滤主对象。所以filterObject中的所有条件都应该匹配。应该返回所有匹配的 id。这可以用“every”来完成


慕尼黑8549860
浏览 85回答 2
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));

慕码人2483693

如果您只想测试一个条件,只需使用该语句并丢弃其余条件,或者如果您想让任何一个条件为真以获得结果,则将 && 逻辑更改为 || 必要的陈述。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]);});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript