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

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

我有一个包含两个字段的对象 filter1filter2 具有数组形式的值

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

数据是对象数组的形式

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"]},
...
]

我必须过滤掉那些具有特定键中存在的任何一个字段的对象 例如,如果 filter{filter1:["mine"]: filter2:["F","D"]},它将首先在数据对象的 filter1 中搜索 filter1 的任何元素,然后搜索存在于数据对象的 filter2 和 [=39 中的 filter2 的任何元素=] 对象,如果找到它们

few example

{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
]

{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
]

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

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

您可以使用 .filter(), .some() and .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);

您可以概括传递的过滤器对象的 by calling some() on the Object.entries(),然后使用嵌套的 some() 调用迭代每个 keyfilter_array

如果您还 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; }

concat() 的这种用法也可以应用于硬编码解决方案,以允许在数据数组中可能不是数组的属性。

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 =>
    [].concat(item.filter1).some(fItem => filter1.includes(fItem)) ||
    [].concat(item.filter2).some(fItem => filter2.includes(fItem))
  ));

const result = search({ filter1: ["mine"], filter2: ["F", "D"] });
console.log(result);