在树结构中排列JSON

时间:2016-03-29 07:25:35

标签: javascript json

我有一个简单的JSON,它包含其从属对象的id。

 var array1=  

    [
        {
            "id": 84,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": null,
            "isRemoved": false,
            "isPrimary": false
        },
        {
            "id": 95,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": 84,
            "isRemoved": false,
            "isPrimary": false
        },
        {
            "id": 150,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": 95,
            "isRemoved": false,
            "isPrimary": false
        },
        {
            "id": 160,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": 95,
            "isRemoved": false,
            "isPrimary": false
        }
    ]

我希望通过识别“id”和“previousOutputTypeActivitySeqMappingId”并将其推入名为的新数组来转换为以下格式     的

var array1=

    [{
        "id": 84,
        "outputTypeId": 900000000000002,
        "previousOutputTypeActivitySeqMappingId": null,
        "isRemoved": false,
        "isPrimary": false,
        "items": [
            {
                "id": 95,
                "outputTypeId": 900000000000002,
                "previousOutputTypeActivitySeqMappingId": 84,
                "isRemoved": false,
                "isPrimary": false,
                "items": [
                    {
                        "id": 150,
                        "outputTypeId": 900000000000002,
                        "previousOutputTypeActivitySeqMappingId": 95,
                        "isRemoved": false,
                        "isPrimary": false,
                        "items": []
                    },
                    {
                        "id": 160,
                        "outputTypeId": 900000000000002,
                        "previousOutputTypeActivitySeqMappingId": 95,
                        "isRemoved": false,
                        "isPrimary": false,
                        "items": []
                    }
                ]
            }
        ]
    }]

我试过的代码如下...... 我首先以新格式创建了一个虚拟对象:

var dummyObj= {
        "id": 84,
        "outputTypeId": 900000000000002,
        "previousOutputTypeActivitySeqMappingId": null,
        "isRemoved": false,
        "isPrimary": false
    }
function populateObj(array1, arrObj) {
   for (var i = 0; i < array1.length; i++) {
        if (array1[i].id == arrObj.parentActivityId) {
            array1[i].items.push(arrObj);
        } else {
            populateObj(array1[i].items, arrObj);
        }
    }
};

populateObj(dummyObj);

因为我不想在开头硬编码虚拟对象。 有没有办法用javascript实现这种转换?
感谢。

2 个答案:

答案 0 :(得分:4)

使用一些函数和一些递归可以很容易地完成。

第一个函数将整个源数组作为源对象,并添加items属性:

function createObject(sourceArray, obj)
{
    obj.items = createArray(sourceArray, obj.id);
  return obj;
}

反过来又使用另一个函数来过滤子元素的源数组:

function createArray(sourceArray, id){
    return sourceArray.filter(function(e){
                                return e.previousOutputTypeActivitySeqMappingId == id;
                             })
                             .map(function(e){
                                 return createObject(sourceArray,e);
                             })
}

你会注意到在tun中回调createObject函数以递归方式继续构建树。

最后一部分是通过查找null父值

的项目来完成所有操作
var result = createArray(input,null);

实例:https://jsfiddle.net/3epjb93j/

答案 1 :(得分:2)

function group(arr) {
    var t={};
    arr.forEach(function(obj) {
        if (!obj.id) throw 'object found without id!';
        t[obj.id]=obj;
    });
    var result=[];
    arr.forEach(function(obj) {
        if (!obj.previousOutputTypeActivitySeqMappingId) {
            result.push(obj);
        } else {
            var parent=t[obj.previousOutputTypeActivitySeqMappingId];
            if (!parent) throw('parent with id '+obj.previousOutputTypeActivitySeqMappingId+' not found!');
            var items=parent.items;
            if (!items) {
                items=[];
                parent.items=items;
            }
            items.push(obj);
        }
    });
    return result;
}