递归javascript函数返回数组

时间:2016-08-28 13:52:12

标签: javascript jquery recursion tree

我有一个对象数组。对象可能有一个名为children的属性,其中inturn将包含一个对象数组。 它实际上是一个物体树。

var my_array_of_objects=[
  {
    "object_info": "blah blah",
    "children": [
      {
        "object_info": "blah blah",
        "children": [
          {
            "object_info": "blah blah"
          }
        ]
      }
    ]
  },
  {
    "object_info": "blah blah"
  },
  {
    "object_info": "blah blah"
  }
]

我试图以递归方式循环遍历my_array_of_objects以返回一个数组,该数组将删除树结构并为每个对象提供一个relationship_id和一个parent_relationship_id,但是我遇到了在递归发生之前设置了relationship_id的问题,我可以& #39;看看为什么......我认为这是一个范围问题?

  function sort_relationships(objects, return_objects, parent_relationship_id, relationship_id){
      $.each( objects, function( field_key, object ){
        relationship_id++;
        var new_object={};
        new_object.relationship_id=relationship_id;
        new_object.parent_relationship_id=parent_relationship_id;
        new_object.object_info=object.object_info;
        // PUSH OBJECT TO THE ARRAY TO BE RETURNED
        return_objects.push(new_object);
        // IF THE OBJECT HAS CHILDREN THEN RUN THE FUNCTION AGAIN WITH IT'S OBJECTS
        if(object.children instanceof Array){
          sort_relationships(object.children,return_objects,new_object.relationship_id,relationship_id);
        }
      });
      return return_objects;
  }

var my_new_array = sort_relationships(my_array_of_objects,[],0,0);

基于上面的代码,我希望每个新对象都有一个唯一的relationship_id,但它会返回以下内容。

[
  {
    "relationship_id": 1,
    "parent_relationship_id": 0,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 2,
    "parent_relationship_id": 1,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 3,
    "parent_relationship_id": 2,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 2,
    "parent_relationship_id": 0,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 3,
    "parent_relationship_id": 0,
    "object_info": "blah blah"
  }
]

我希望有以下内容:

[
  {
    "relationship_id": 1,
    "parent_relationship_id": 0,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 2,
    "parent_relationship_id": 1,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 3,
    "parent_relationship_id": 2,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 4,
    "parent_relationship_id": 0,
    "object_info": "blah blah"
  },
  {
    "relationship_id": 5,
    "parent_relationship_id": 0,
    "object_info": "blah blah"
  }
]

2 个答案:

答案 0 :(得分:1)

您正在对每个递归调用的本地变量执行relationship_id++,并且不会在调用者处反映出来。您需要return更新的值,或者只使用自由变量:

function sort_relationships(objects) {
    var return_objects = [];
    var relationship_id = 0;
    function recurse(objects, parent_relationship_id) {
        $.each(objects, function(field_key, object) {
            relationship_id++;
            var new_object = {
                relationship_id: relationship_id,
                parent_relationship_id: parent_relationship_id,
                object_info: object.object_info
            };
            // PUSH OBJECT TO THE ARRAY TO BE RETURNED
            return_objects.push(new_object);
            // IF THE OBJECT HAS CHILDREN THEN RUN THE FUNCTION AGAIN WITH IT'S OBJECTS
            if (Array.isArray(object.children)) {
                recurse(object.children, new_object.relationship_id);
            }
        });
    }
    recurse(objects, 0);
    return return_objects;
}

var my_new_array=sort_relationships(my_array_of_objects);

答案 1 :(得分:1)

你可以使用reduce,对于id(出错的地方),你可以向父id添加已经累积的对象的数量:

function flatten(arr, parentId = 0) {
    if (!arr) return [];
    return arr.reduce ( function (acc, obj) {
        var rel = {
            object_info: obj.object_info,
            relationship_id: parentId + acc.length + 1,
            parent_relationship_id: parentId
        };
        return acc.concat([rel], flatten(obj.children, rel.relationship_id));
    }, []);
}

// sample data
var my_array_of_objects=[{
    "object_info": "blah blah",
    "children": [{
        "object_info": "blah blah",
        "children": [{
            "object_info": "blah blah"
          }
        ]
      }
    ]
  }, {
    "object_info": "blah blah"
  }, {
    "object_info": "blah blah"
  }
];

// convert
var result = flatten(my_array_of_objects);
// output
console.log(result);