简体   繁体   English

按子项过滤并在嵌套的对象数组中返回父项和子项

[英]Filter by children and return parent plus children in a nested array of objects

I have an array of objects like the one below and I'm looking to get the parent (Dashboard) and the name of its child when searching for 'Summary'我有一个像下面这样的对象数组,我希望在搜索“摘要”时获取父级(仪表板)及其子级的名称

[
  {
     Dashboard1: [
       {name: 'Summary'},
       {name: 'SomeThingElse},
       {name: 'SomeThingElse2}
     ]
  },
  {
     Dashboard2: [
       {name: 'Summary'},
       {name: 'Different},
       {name: 'Different2}
     ]
  },
  {
     Dashboard3: [
       {name: 'Summary'},
       {name: 'AnotherDifferent},
       {name: 'AnotherDifferent2}
     ]
  }
  {
     Dashboard4: [
       {name: 'AnotherDifferent},
       {name: 'AnotherDifferent2}
       {name: 'AnotherDifferent3},
       {name: 'AnotherDifferent4},
     ]
  }
]

I have an input that searches for 'Summary' and I want it to return just我有一个搜索“摘要”的输入,我希望它只返回

[
  {
    Dashboard1: [
      {name: 'Summary'},
    ]
  },
  {
    Dashboard2: [
      {name: 'Summary'},
    ]
  },
  {
    Dashboard2: [
      {name: 'Summary'},
    ]
  }
]

I have this function that filters Dashboard4 where Summary is not present as a child but returns all children as well.我有这个功能可以过滤 Dashboard4,其中摘要不作为子项存在,但也会返回所有子项。

function filterByValue(array, value) {
    return array.filter((data) =>  JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1);
}

Thanks!谢谢!

 const a = [ { Dashboard1: [ {name: 'Summary'}, {name: 'SomeThingElse'}, {name: 'SomeThingElse2'} ] }, { Dashboard2: [ {name: 'Summary'}, {name: 'Different'}, {name: 'Different2'} ] }, { Dashboard3: [ {name: 'Summary'}, {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'} ] }, { Dashboard4: [ {name: 'AnotherDifferent'}, {name: 'AnotherDifferent2'}, {name: 'AnotherDifferent3'}, {name: 'AnotherDifferent4'}, ] } ]; function filterByValue(array, value) { const result = []; array.forEach(e => { const keys = Object.keys(e); const obj = {}; keys.forEach(k => { const matches = e[k].filter(i => i.name === value); if (matches.length > 0) { obj[k] = matches; } }); if (Object.keys(obj).length > 0) { result.push(obj); } }); return result; } console.log(filterByValue(a, 'Summary'));

Given the array, you can use a more coincise function like that:给定数组,您可以使用更巧合的函数:

    const array = [
  {
     Dashboard1: [
       {name: 'Summary'},
       {name: 'SomeThingElse'},
       {name: 'SomeThingElse2'}
     ]
  },
  {
     Dashboard2: [
       {name: 'Summary'},
       {name: 'Different'},
       {name: 'Different2'}
     ]
  },
  {
     Dashboard3: [
       {name: 'Summary'},
       {name: 'AnotherDifferent'},
       {name: 'AnotherDifferent2'}
     ]
  },
  {
     Dashboard4: [
       {name: 'AnotherDifferent'},
       {name: 'AnotherDifferent2'},
       {name: 'AnotherDifferent3'},
       {name: 'AnotherDifferent4'},
     ]
  }]

const filterByValue = (array, value) => array.filter(el => {
    const objValues = Object.values(el).flat()
    const elementsValues = objValues.map(v => Object.values(v)).flat()
    return elementsValues.some(v => v.includes(value))
})

console.log(filterByValue(array, 'Summary'));

This is a reusable function for a similar scenario.这是类似场景的可重用功能。

const searchNestedArraySearch = (val, arr, key, childKey) => {

const newArr = arr.map((e, i) => { const newArr = arr.map((e, i) => {

//Get the propert need from nested object via the key and appending the index
let firstChildObj = e[`${key}${i + 1}`];

console.log(firstChildObj);

// loop through the nested property array
const checkProperty = firstChildObj.map((e) => {
  const valueProp = e[childKey] === val ? true : false;

  if (valueProp) {
    return { [childKey]: val };
  } else {
    return null;
  }
});

// remove null value from each array to get specified search
const filterEmpty = checkProperty.filter((val) => val !== null);

// return array of property containing search value
if (filterEmpty.length) {
  return { [`${key}${i + 1}`]: filterEmpty };
}

}); });

return newArr.filter((e) => e !== undefined); return newArr.filter((e) => e !== undefined); }; };

console.log(searchNestedArray("Summary", data, "Dashboard", "name")); console.log(searchNestedArray("Summary", data, "Dashboard", "name"));

/** /**

  • Filter value from nested object从嵌套对象过滤值
  • @param {String} val @param {String} val
  • @param {Array} arr @param {Array} arr
  • @param {string} key @param {string} 键
  • @param {String} childKey */ @param {String} childKey */

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

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