Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为以下Javascript对象创建嵌套对象?_Javascript_Arrays_Json_Object_Nested Loops - Fatal编程技术网

为以下Javascript对象创建嵌套对象?

为以下Javascript对象创建嵌套对象?,javascript,arrays,json,object,nested-loops,Javascript,Arrays,Json,Object,Nested Loops,我想构建一个类似于以下结构的对象: record 1 {id,name,desc} record 2 {id,name,desc} record 3 {id,name,desc} record 4 {id,name,desc} 它是一个嵌套结构,其中记录1是父记录,记录2是记录1的子记录,记录3是记录2的子记录,依此类推 我在StackOverflow中读过很多文章,其中建议使用我无法正确生成的方法,如push、put等。请帮忙 目标 不确定其顺序

我想构建一个类似于以下结构的对象:

record 1 {id,name,desc}
   record 2 {id,name,desc}
        record 3 {id,name,desc}
            record 4 {id,name,desc}
它是一个嵌套结构,其中记录1是父记录,记录2是记录1的子记录,记录3是记录2的子记录,依此类推

我在StackOverflow中读过很多文章,其中建议使用我无法正确生成的方法,如push、put等。请帮忙

目标

不确定其顺序是否正确,但其想法是将以下对象转换为嵌套对象:

[
  {
    "name": "Level 2",
    "childId": "caf23c95db3110100cc4bd513996195d",
    "parentId": "8b9e235c0fe412004e938fbce1050e0f",
    "description": null,
    "level": 1
  },
  {
    "name": "Level 3",
    "childId": "c303f495db3110100cc4bd51399619b8",
    "parentId": "caf23c95db3110100cc4bd513996195d",
    "description": null,
    "level": 2
  },
  {
    "name": "Level 4",
    "childId": "be133895db3110100cc4bd51399619ba",
    "parentId": "c303f495db3110100cc4bd51399619b8",
    "description": null,
    "level": 3
  },
  {
    "name": "Intrusion and Incident Response Standard Operating Procedure",
    "id": "8b9e235c0fe412004e938fbce1050e0f",
    "description": "blablalblablabab",
    "level": 0
  }
]

使用此代码

函数hasChild(父级,级别){
var grProcessChild=新记录(“sn_合规性_政策”);
grProcessChild.addQuery('parent',parent);
grProcessChild.query();
while(grProcessChild.next()){
变量级别=parseInt(级别)+1;
var child={};//对象
child.name=grProcessChild.getValue('name');
child.childId=grProcessChild.getUniqueValue();
child.parentId=grProcessChild.getValue('parent');
child.description=grProcessChild.getValue('description');
child.level=级别;
arrData.push(child);
hasChild(grProcessChild.getUniqueValue(),级别);
}
}
var arrData=[];//排列
var grProcess=新记录(“sn_合规性_政策”);
grProcess.addQuery('sys_id','8b9e235c0fe412004e938fbce1050e0f');
grProcess.query();
while(grProcess.next()){
var root={};//对象
root.name=grProcess.getValue('name');
root.id=grProcess.getUniqueValue();
root.description=grProcess.getValue('description');
root.level=0;
hasChild(grProcess.getUniqueValue(),root.level);
arrData.push(根);

}
如果您使用的是lodash,那么还原光方法将非常有用。 这是一个证明它工作的例子。

请提供一个例子。
[
  {
    "name": "Level 2",
    "childId": "caf23c95db3110100cc4bd513996195d",
    "parentId": "8b9e235c0fe412004e938fbce1050e0f",
    "description": null,
    "level": 1
  },
  {
    "name": "Level 3",
    "childId": "c303f495db3110100cc4bd51399619b8",
    "parentId": "caf23c95db3110100cc4bd513996195d",
    "description": null,
    "level": 2
  },
  {
    "name": "Level 4",
    "childId": "be133895db3110100cc4bd51399619ba",
    "parentId": "c303f495db3110100cc4bd51399619b8",
    "description": null,
    "level": 3
  },
  {
    "name": "Intrusion and Incident Response Standard Operating Procedure",
    "id": "8b9e235c0fe412004e938fbce1050e0f",
    "description": "blablalblablabab",
    "level": 0
  }
]