简体   繁体   English

递归地重新创建嵌套对象的数组

[英]Recreate an array of nested objects recursively

I have an array of nested objects created by a user. 我有一个由用户创建的嵌套对象数组。 I need to recursively check the array for if checked is set to true and recreate the nested array of objects as shown below. 我需要递归检查数组,如果将checked设置为true并重新创建嵌套的对象数组,如下所示。

With my original attempt I was able to recursively get through the nested array. 通过最初的尝试,我能够递归地访问嵌套数组。 However, every time it recursed I would overwrite my previous information. 但是,每次重复出现时,我都会覆盖以前的信息。

$scope.newPropertyTemplate = [];

function checkProps (item, props){
    debugger
    var obj = {};
    var arr = [];

    for(var i in props){

        if(props[i].checked === true){
            obj.property = item;
            arr.push(props[i].name);
            obj.subProperties = arr;
        }

        if(props[i].properties){
            checkProps(props[i].name, props[i].properties);
        }
    }

    return obj;
}

for(var i = 0; i < $scope.propertyTemplate.length; i++){

    if($scope.propertyTemplate[i].properties !== null){

        $scope.newPropertyTemplate.push(checkProps($scope.propertyTemplate[i].name, $scope.propertyTemplate[i].properties));
        // $scope.newPropertyTemplate.push( newItem.property = $scope.propertyTemplate[i].name );
    }
    else {
        $scope.newPropertyTemplate.push($scope.propertyTemplate[i].name);
    }
}

Here's an example of the nested array: 这是嵌套数组的示例:

    [
     {name: "allocation",
      properties:[
      {    
          name: "oid", 
          checked: true, 
          properties: null, 
          type: "integer"
      },
      {
          name: "person", 
          checked: false, 
          properties: [
              {
                  name: "Expires",
                  checked: true,
                  properties: null,
                  type: "timestamp"
              },
              {
                  name: "State/Province",
                  checked: true,
                  properties: null,
                  type: "string"
              }
          ]
      ],
      type: "allocation"
    ]

Here's an example of the converted array of objects: 这是转换后的对象数组的示例:

[
 {property: "allocation",
  subProperties: [
      "oid",
      {property: "person",
       subProperties: [
           "Expires",
           "State/Province"
       ]
      }
  ]
 }
]

This should work. 这应该工作。 The returned obj from your recursive checkProps function was not being stored. 您的递归checkProps函数返回的obj没有被存储。

 var data = [{ name: "allocation", properties: [{ name: "oid", checked: true, properties: null, type: "integer" }, { name: "person", checked: false, properties: [{ name: "Expires", checked: true, properties: null, type: "timestamp" }, { name: "State/Province", checked: true, properties: null, type: "string" }] }, ], type: "allocation" }]; function process(paths) { var arr = []; data.forEach(function(template) { var resultObj = {}; recurse(template, resultObj); arr.push(resultObj); }); console.log(arr); } function recurse(template, obj) { obj.property = template.name; if (template.properties == null) return; obj.subProperties = []; template.properties.forEach(function(prop) { var res = {}; if (prop.properties != null) recurse(prop, res); else if (prop.checked) res = prop.name; if (res) obj.subProperties.push(res); });; } process(data); 

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

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