如何从JavaScript对象创建树状结构

时间:2018-11-16 11:46:07

标签: javascript json object

这是我得到的对象样本:

输入:

  

{       “人”:[           {               “ id”:“ 12”,               “ parentId”:“ 0”,               “ text”:“ Man”,               “1级”,               “儿童”:空           },           {               “ id”:“ 6”,               “ parentId”:“ 12”,               “ text”:“ Boy”,               “级别”:“ 2”,               “儿童”:空           },                   {               “ id”:“ 7”,               “ parentId”:“ 12”,               “ text”:“ Other”,               “级别”:“ 2”,               “儿童”:空           },           {               “ id”:“ 9”,               “ parentId”:“ 0”,               “ text”:“ woman”,               “1级”,               “儿童”:空           },           {               “ id”:“ 11”,               “ parentId”:“ 9”,               “ text”:“ Girl”,               “级别”:“ 2”,               “儿童”:空           }       ]}

我想将其转换为这样的JSON格式:

{
    "People": [
        {
            "id": "12",
            "parentId": "0",
            "text": "Man",
            "level": "1",
            "children": [
                {
                    "id": "6",
                    "parentId": "12",
                    "text": "Boy",
                    "level": "2",
                    "children": null
                },
                {
                    "id": "7",
                    "parentId": "12",
                    "text": "Other",
                    "level": "2",
                    "children": null
                }   
            ]
        }
}

任何想法/帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您可以使用对象作为对孩子或父母的引用,并收集孩子和父母以仅获得该人作为根。

var data = { John: "James", Samar: "Michel", Albert: "Michel", Michel: "James", James: "Sarah" },
    parents = new Set,
    children = new Set,
    references = {},
    result;

Object
    .entries(data)
    .forEach(([child, parent]) => {
        references[child] = references[child] || [];
        references[parent] = references[parent] || [];
        references[parent].push({ [child]: references[child] });
        parents.add(parent);
        children.add(child);
    });

result = [...parents]
    .filter(p => !children.has(p))
    .map(p => ({ [p]: references[p] }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

天哪,看似简单的东西花了很长时间

const users = {
        "John": "James",
        "Samar": "Michel",
        "Albert": "Michel",
        "Michel": "James",
        "James": "Sarah"
    }
    
    const findRoots = () => Object.keys(users).filter(k => !(users[k] in users)).map(k => users[k])
    
    const findSubordinates = (boss) => Object.keys(users).filter(k => users[k] === boss)
    
    const traverseBoss = (boss) => {
        let subs = findSubordinates(boss)
    
        let subsCollection = []
    
        subs.forEach(s => {
            subsCollection.push({
                [s]: traverseBoss(s)
            })
        })
    
        return subsCollection
    }
    
    
    const result = {}
    findRoots().forEach(root => result[root] = traverseBoss(root))
    
    console.log(result)