将平面嵌套数据转换为单个数组

converting a flat nested data into single array

我有一个嵌套 child 的数据数组,如下所示:

var Tabledata  = [{
"id" : 2,
"children" : [{
    "id" : 3,
    "children" : [{
        "id" : 5,
        "children" : []
    }, {
        "id" : 7,

        "children" : []
    }]
}]

我想将所有具有 parent 个 ID 或仅 child 个 ID 的所有 child 个 ID 放入一个数组中。 喜欢 ids = [2,3,5,7]

或者我有来自 mysql table 的完整数据如下:

data = [{
"id" : 7,
"department_name" : "newupdate",
"display_name" : "newupfate",
"description" : "new",
"parent_department_id" : 3,
"Is_child_company" : true,
"status" : true,
"created_on" : "2015-03-17T06:24:45.000Z",
"created_by" : 1
}, {
"id" : 5,
"department_name" : "First department child1s child1s child",
"display_name" : "saFDC1C1C",
"description" : "",
"parent_department_id" : 3,
"Is_child_company" : true,
"status" : true,
"created_on" : "2015-02-17T12:50:14.000Z",
"created_by" : 1
 }, {
"id" : 4,
"department_name" : "Second department",
"display_name" : "SD",
"description" : "",
"parent_department_id" : null,
"Is_child_company" : false,
"status" : true,
"created_on" : "2015-02-17T12:50:14.000Z",
"created_by" : 1
}, {
"id" : 3,
"department_name" : "First department child1s child",
"display_name" : "FDC1C1",
"description" : "",
"parent_department_id" : 2,
"Is_child_company" : true,
"status" : true,
"created_on" : "2015-02-17T12:50:14.000Z",
"created_by" : 1
}, {
"id" : 2,
"department_name" : "First department child1",
"display_name" : "FDC1",
"description" : "",
"parent_department_id" : 1,
"Is_child_company" : true,
"status" : true,
"created_on" : "2015-02-17T12:50:14.000Z",
"created_by" : 1
}, {
"id" : 1,
"department_name" : "First Department",
"display_name" : "FD",
"description" : "",
"parent_department_id" : null,
"Is_child_company" : false,
"status" : true,
"created_on" : "2015-04-14T06:55:24.000Z",
"created_by" : 1
 }]    

如果我有一个 parent id =2 然后想要得到任何 depth.like childs =[3 的所有嵌套 child id ,5,7]

您可以尝试这样的操作:

function findId(data) {
    var arr = [];

    (function req(data) {
        data.forEach(function(item) {
            arr.push(item.id);
            if (item.children) {
               req(item.children);
            }
        })
    })(data)

    return arr;
}

DEMO

    var data = [{
        "id": 2,
        "children": [{
      "id": 3,
     "children": [{
       "id": 5,
       "children": []
     }, {
       "id": 7,

       "children": []
     }]
   }]
    }];

var flatData =[];

function flat(fData){
    $(fData).each(function(){
        flatData.push($(this)[0].id);
        if($(this)[0].children.length>0){
            ($(this)[0].children)
        }
    });
 }

 $(document).ready(function(){
     flat(data);
     console.log(flatData);
 });

http://jsfiddle.net/Lhxz1spL/

在服务器端节点 js 使用下划线 js 模块,答案将是:

 var data = [{
 "id" : 2,

 "children" : [{
    "id" : 3,

    "children" : [{
        "id" : 5,

        "children" : []
    }, {
        "id" : 7,

        "children" : []
       }]
     }]
  }];

 var flatIds=[];

 function myfunc(data) {
            _.each(data, function(item) {
                            flatIds.push(item.id);
                            if (item.children.length > 0) {
                                myfunc(item.children)
                            }

                })

}

myfunc(data);

console.log(flatIds)    // [ 2, 3, 5, 7 ]