简体   繁体   English

根据以数组形式存在于对象值中的值搜索对象数组

[英]searching an array of objects on the basis of value present inside an value of an object in form of array

I have an an object with two fields filter1 and filter2 having values in form of array我有一个带有两个字段filter1filter2的对象,它们的值是数组形式

let filter = {filter1:["mine","your"]: filter2:["C","D"]} //values not fixed let filter = {filter1:["mine","your"]: filter2:["C","D"]} //值不固定

the data is in form of array of objects数据是对象数组的形式

let data = [
{ id:1, filter1:["mine"], filter2:["E","C"]},
{ id:2, filter1:["mine"], filter2:["E","C","F"]},
{ id:3, filter1:["your"], filter2:["C"]},
{ id:3, filter1:["your"], filter2:["D","C"]},
{ id:5, filter1:["other"], filter2:["F"]},
...
]

I have to filter out those objects which have any one of the fields present in specific key for eg if filter is {filter1:["mine"]: filter2:["F","D"]} it will first search for any element of filter1 in filter1 of data object and after that search for any element of filter2 which is present in the filter2 of data object and return the object if any one them is found我必须过滤掉那些在特定键中存在任何一个字段的对象,例如,如果filter{filter1:["mine"]: filter2:["F","D"]}它将首先搜索任何数据对象的过滤器1中过滤器1的元素,然后搜索数据对象过滤器2中存在的过滤器2的任何元素,如果找到任何一个,则返回该对象

few example

result for {filter1:["mine"]: filter2:["F","D"]} {filter1:["mine"]: filter2:["F","D"]}

result = [
{ id:1, filter1:["mine"], filter2:["E","C"]}, //since filter1 "mine"
{ id:2, filter1:["mine"], filter2:["E","C","F"]}, //since filter1 "mine"
{ id:3, filter1:["your"], filter2:["D","C"]}, //since from "F" and "D" from filter2 "D" is present
{ id:5, filter1:["other"], filter2:["F"]}, //since "F" from filter2 is present
]

result for {filter1:["your"]: filter2:["F","G"]} {filter1:["your"]: filter2:["F","G"]}

result = [
{ id:2, filter1:["mine"], filter2:["E","C","F"]}, //since "F" from filter2 is present
{ id:3, filter1:["your"], filter2:["D","C"]}, //since filter1 is "your"
{ id:5, filter1:["other"], filter2:["F"]}, //since "F" from filter2 is present
]

result for {filter1:[]: filter2:["D"]} {filter1:[]: filter2:["D"]}

result = [
{ id:3, filter1:["your"], filter2:["D","C"]}, //since filter2 has "D"
]

You can use a combination of .filter() , .some() and .includes() :您可以组合使用.filter().some().includes()

 const data = [ { id:1, filter1:["mine"], filter2:["E","C"]}, { id:2, filter1:["mine"], filter2:["E","C","F"]}, { id:3, filter1:["your"], filter2:["C"]}, { id:3, filter1:["your"], filter2:["D","C"]}, { id:5, filter1:["other"], filter2:["F"]} ]; const search = ({ filter1, filter2 }) => data.filter(item => item.filter1.some((fitem) => filter1.includes(fitem)) || item.filter2.some((fitem) => filter2.includes(fitem)) ) const result = search({filter1:["mine"], filter2:["F","D"]}); console.log(result);

You can generalize the solution proposed by RoMilton by calling some() on the Object.entries() of the passed filter object, and then iterating each key and filter_array with nested some() calls.您可以通过在传递的过滤器对象的Object.entries() some()上调用some() ,然后使用嵌套的some()调用迭代每个keyfilter_array来概括RoMilton 提出解决方案

If you also Array#concat() the currently iterated data element property into an array you can include non-array properties in the filter object ie id in this case.如果您还将Array#concat()当前迭代的数据元素属性放入数组中,则可以在过滤器对象中包含非数组属性,即在这种情况下为id

 const data = [ { id: 1, filter1: ["mine"], filter2: ["E", "C"] }, { id: 2, filter1: ["mine"], filter2: ["E", "C", "F"] }, { id: 3, filter1: ["your"], filter2: ["C"] }, { id: 3, filter1: ["your"], filter2: ["D", "C"] }, { id: 5, filter1: ["other"], filter2: ["F"] } ]; const search = (array, filter_object) => array.filter(item => Object.entries(filter_object).some(([key, filter_array]) => [].concat(item[key]).some(fitem => filter_array.includes(fitem))) ); const filter = { filter1: ["mine"], filter2: ["F", "D"] }; const result = search(data, filter); console.log(...result.map(({ id }) => ({ id }))); const filter2 = { id: [5], filter1: ["mine"] } const result2 = search(data, filter2); console.log(...result2.map(({ id }) => ({ id })));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM