简体   繁体   English

基于条件检查迭代每个对象数组

[英]Iterate through each object array based on condition check

I have a scenario were I need to iterate through each parent/child array in an object. 我有一个场景是我需要迭代对象中的每个父/子数组。

Each Grandparents can have multiple parents, in same fashion each parent can have multiple childs, each child can have multiple subChilds and so on. 每个祖父母可以有多个父母,以同样的方式,每个父母可以有多个孩子,每个孩子可以有多个子孩子等等。

I need to check if type is "parent" or "child" while iterating and then push name property to an array as mentioned in expected output. 我需要在迭代时检查类型是“父”还是“子”,然后将name属性推送到预期输出中提到的数组。

Input Object: 输入对象:

 var inputObject = {
  "id": 1,
  "name": "Grand Parent 1",
  "type": "GrandParent",
  "childType": [
   {
     "id": 2,
     "type": "Parent",
     "childType": [
    {
      "id": 3,
      "type": "Child",
      "childType": [],
      "name": "Child 11"
    },
    {
      "id": 4,
      "type": "Child",
      "childType": [],
      "name": "Child 12"
    }
  ],
  "name": "Parent 1"
},
{
  "id": 5,
  "type": "Parent",
  "childType": [
    {
      "id": 6,
      "type": "Child",
      "childType": [],
      "name": "Child 21"
    }
  ],
  "name": "Parent 2"
},
{
  "id": 7,
  "type": "Parent",
  "childType": [
    {
      "id": 8,
      "type": "Child",
      "childType": [],
      "name": "Child 31"
    }
  ],
  "name": "Parent 3"
}
 ]
 }

Code Tried: 代码尝试:

 function handleData({childType, ...rest}){
  const res = [];
  res.push(rest.name);
  if(childType){
  if(rest.type == "Child")
    res.push(...handleData(childType));
  }
  return res;
}

const res = handleData(inputObject);

Expected Output: 预期产出:

If type selected is "Parent"
["Parent 1", "Parent 2, Parent 3"]

if type selected is "Child"
["Child 11", "Child 12", "Child 21", "Child 31"]

You can do a recursive function that uses flatMap() : 你可以做一个使用flatMap()的递归函数:

 const obj = {id:1,name:"Grand Parent 1",type:"GrandParent",childType:[{id:2,type:"Parent",childType:[{id:3,type:"Child",childType:[],name:"Child 11"},{id:4,type:"Child",childType:[],name:"Child 12"}],name:"Parent 1"},{id:5,type:"Parent",childType:[{id:6,type:"Child",childType:[],name:"Child 21"}],name:"Parent 2"},{id:7,type:"Parent",childType:[{id:8,type:"Child",childType:[],name:"Child 31"}],name:"Parent 3"}]}; const get = (o, t) => o.type === t ? [o.name] : o.childType.flatMap(c => get(c, t)); console.log(get(obj, 'GrandParent')); console.log(get(obj, 'Parent')); console.log(get(obj, 'Child')); 

You can do that using recursion. 你可以使用递归来做到这一点。

Steps: 脚步:

  • Create a wrapper function in which you declare result array. 创建一个包装器函数,在其中声明结果数组。
  • Inside that make a wrapper function create a function which would be called recursively 在内部使包装器函数创建一个可以递归调用的函数
  • Check if type matches the given type add the name of object to result 检查类型是否与给定类型匹配将对象name添加到result
  • Then check if elements exists in its childType . 然后检查其childType是否存在元素。 If yes yes then call function on each each of its element. 如果是,则在每个元素上调用函数。

 var inputObject = { "id": 1, "name": "Grand Parent 1", "type": "GrandParent", "childType": [ { "id": 2, "type": "Parent", "childType": [ { "id": 3, "type": "Child", "childType": [], "name": "Child 11" }, { "id": 4, "type": "Child", "childType": [], "name": "Child 12" } ], "name": "Parent 1" }, { "id": 5, "type": "Parent", "childType": [ { "id": 6, "type": "Child", "childType": [], "name": "Child 21" } ], "name": "Parent 2" }, { "id": 7, "type": "Parent", "childType": [ { "id": 8, "type": "Child", "childType": [], "name": "Child 31" } ], "name": "Parent 3" } ] } function handleData(obj,type){ let res = []; function recursive(obj){ if(type === obj.type) res.push(obj.name); if(obj.childType.length){ obj.childType.forEach(a => recursive(a)); } } recursive(obj) return res; } console.log(handleData(inputObject,"Child")) console.log(handleData(inputObject,"Parent")) 

Recursion is elegant but you could use es6 to do it , if the object childType was very large, recursion might not be applicable (stack overflow), here is a solution using reduce, 递归很优雅,但你可以使用es6来做,如果对象childType非常大,递归可能不适用(堆栈溢出),这里是一个使用reduce的解决方案,

function getType({childType}, type) {
  return childType.reduce( (acc, {type: objectType, name}) => { 
   if (objectType === type){
     acc.push(name)
   }
   return acc
  }, [])
}

You can solve this problem using recursion. 您可以使用递归来解决此问题。 You can try on this way ! 你可以尝试这种方式!

 var inputObject = { "id": 1, "name": "Grand Parent 1", "type": "GrandParent", "childType": [ { "id": 2, "type": "Parent", "childType": [ { "id": 3, "type": "Child", "childType": [], "name": "Child 11" }, { "id": 4, "type": "Child", "childType": [], "name": "Child 12" } ], "name": "Parent 1" }, { "id": 5, "type": "Parent", "childType": [ { "id": 6, "type": "Child", "childType": [], "name": "Child 21" } ], "name": "Parent 2" }, { "id": 7, "type": "Parent", "childType": [ { "id": 8, "type": "Child", "childType": [], "name": "Child 31" } ], "name": "Parent 3" } ] }; var resultValues = []; function getResult(inputObject, propertyName, propertyValue) { for (var objectProperty in inputObject) { if (objectProperty == propertyName && inputObject[objectProperty] == propertyValue) { resultValues.push(inputObject['name']); console.log(resultValues); } else { if (objectProperty == 'childType') { inputObject[objectProperty].forEach(function (element) { getResult(element, propertyName, propertyValue) }); } } } //console.log(resultValues); } getResult(inputObject, 'type', 'GrandParent'); getResult(inputObject, 'type', 'Parent'); getResult(inputObject, 'type', 'Child'); 

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

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