es6 - javascript merge multiple JSON objects into one (with child-parent relationship)
PHP中文网
PHP中文网 2017-06-30 09:58:35
0
3
1782

1. Currently there is a very arbitrary interface API, and the data it provides is very unreasonable
2. The data is as follows

var json = [{"mainId": 1, "title": "abc",  "createdate": "2017-06-28"},{"mainId": 2, "title": "ddddd",  "createdate": "2017-06-25"}];
var json1 = [{"childId":1, "mainId": 1, "childname": "cname"},{"childId":2, "mainId": 1, "childname": "cname2"},{"childId":3, "mainId": 2, "childname": "cname3"},{"childId":4, "mainId": 2, "childname": "cname4"}];
var json2 = [{"childchildId":1, "childId": 1, "childname": "cname"},{"childchildId":2, "childId": 1, "childname": "cname2"},{"childchildId":3, "childId": 2, "childname": "cname3"},{"childchildId":4, "childId": 2, "childname": "cname4"},{"childchildId":5, "childId": 3, "childname": "cname"},{"childchildId":6, "childId": 3, "childname": "cname2"},{"childchildId":7, "childId": 4, "childname": "cname3"},{"childchildId":8, "childId": 4, "childname": "cname4"}];

3. I want to merge them into a json as follows:

var result = [{"mainId":1, "title": "abc", "createdate": "2017-06-28", child:[{"childId":1, "mainId": 1, "childname": "cname", childchild:[{"childchildId":1, "childId": 1, "childname": "cname"},{"childchildId":2, "childId": 1, "childname": "cname2"}]},{"childId":2, "mainId": 1, "childname": "cname2", childchild:[{"childchildId":3, "childId": 2, "childname": "cname3"},{"childchildId":4, "childId": 2, "childname": "cname4"}]}]

Is there a good way?

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
洪涛

Well, I just don’t want to write a loop


let json2Obj = json2.reduce((acc, cur) => {
    let childId = cur.childId;
    if (!acc[childId]) {
        acc[childId] = [];
    }
    acc[childId].push(cur);
    return acc;
}, {})

json1.forEach(item => {
    item.childchild = json2Obj[item.childId]
})

let json1Obj = json1.reduce((acc, cur) => {
    let mainId = cur.mainId;
    if (!acc[mainId]) {
        acc[mainId] = [];
    }
    acc[mainId].push(cur);
    return acc;
}, {})

json.forEach(item => {
    item.child = json1Obj[item.mainId];
})

console.log(JSON.stringify(json));

Reduce reconstruction:


const toObj = (json, idStr) => json.reduce((acc, cur) => {
    let id = cur[idStr];
    if (!acc[id]) {
        acc[id] = [];
    }
    acc[id].push(cur);
    return acc;
}, {})

const json2Obj = toObj(json2, 'childId');
json1.forEach(item => item.childchild = json2Obj[item.childId])
const json1Obj = toObj(json1, 'mainId');
json.forEach(item => item.child = json1Obj[item.mainId])

console.log(JSON.stringify(json));
滿天的星座

php, step by step

header('content-type:application/json;charset=utf8');

$json = '[{"mainId": 1, "title": "abc",  "createdate": "2017-06-28"},{"mainId": 2, "title": "ddddd",  "createdate": "2017-06-25"}]';
$json1 = '[{"childId":1, "mainId": 1, "childname": "cname"},{"childId":2, "mainId": 1, "childname": "cname2"},{"childId":3, "mainId": 2, "childname": "cname3"},{"childId":4, "mainId": 2, "childname": "cname4"}]';
$json2 = '[{"childchildId":1, "childId": 1, "childname": "cname"},{"childchildId":2, "childId": 1, "childname": "cname2"},{"childchildId":3, "childId": 2, "childname": "cname3"},{"childchildId":4, "childId": 2, "childname": "cname4"},{"childchildId":5, "childId": 3, "childname": "cname"},{"childchildId":6, "childId": 3, "childname": "cname2"},{"childchildId":7, "childId": 4, "childname": "cname3"},{"childchildId":8, "childId": 4, "childname": "cname4"}]';

$arr = array_merge(json_decode($json, true), json_decode($json1, true), json_decode($json2, true));

print_r(json_encode(dataFormat($arr)));

//整理数据格式
function dataFormat($arr){
    foreach ($arr as $key => &$value) {
        $value['id'] = $key;
    }

    foreach ($arr as $key => &$value) {
        if (!empty($value['mainId']) && empty($value['childId'])) {
            $value['pid'] = '100';
        }

        if (!empty($value['mainId']) && !empty($value['childId'])) {
            foreach ($arr as $k => $v) {
                if (!empty($v['mainId']) && empty($v['childId'])) {
                    if ($value['mainId'] == $v['mainId']) {
                        $value['pid'] = $k;
                    }
                }
            }
        }

        if (!empty($value['childchildId'])) {
            foreach ($arr as $k => $v) {

                if (!empty($v['mainId']) && !empty($v['childId'])) {
                    if ($value['childId'] == $v['childId']) {
                        $value['pid'] = $k;
                    }
                }
            }
        }
    }

    //生成树
    $arr = getTree($arr);

    //删除id、pid
    deleteKey($arr, 'id', 'pid');

    return $arr;
    //删除不需要的键id、pid
}

//获取树
function getTree($items){
    $tree = array();
    foreach($items as $item){
        if(isset($items[$item['pid']])){
            $items[$item['pid']]['child'][] = &$items[$item['id']];
        }else{
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}

//删除不需要的键
function deleteKey(&$arr, $id, $pid){
    foreach ($arr as $key => &$value) {
        if (!empty($value['child']) && is_array($value['child'])) {
            deleteKey($value['child'], $id, $pid);
        }
        unset($value[$id], $value[$pid]);
    }
}

Normal output

[
    {
        "mainId": 1,
        "title": "abc",
        "createdate": "2017-06-28",
        "child": [
            {
                "childId": 1,
                "mainId": 1,
                "childname": "cname",
                "child": [
                    {
                        "childchildId": 1,
                        "childId": 1,
                        "childname": "cname"
                    },
                    {
                        "childchildId": 2,
                        "childId": 1,
                        "childname": "cname2"
                    }
                ]
            },
            {
                "childId": 2,
                "mainId": 1,
                "childname": "cname2",
                "child": [
                    {
                        "childchildId": 3,
                        "childId": 2,
                        "childname": "cname3"
                    },
                    {
                        "childchildId": 4,
                        "childId": 2,
                        "childname": "cname4"
                    }
                ]
            }
        ]
    },
    {
        "mainId": 2,
        "title": "ddddd",
        "createdate": "2017-06-25",
        "child": [
            {
                "childId": 3,
                "mainId": 2,
                "childname": "cname3",
                "child": [
                    {
                        "childchildId": 5,
                        "childId": 3,
                        "childname": "cname"
                    },
                    {
                        "childchildId": 6,
                        "childId": 3,
                        "childname": "cname2"
                    }
                ]
            },
            {
                "childId": 4,
                "mainId": 2,
                "childname": "cname4",
                "child": [
                    {
                        "childchildId": 7,
                        "childId": 4,
                        "childname": "cname3"
                    },
                    {
                        "childchildId": 8,
                        "childId": 4,
                        "childname": "cname4"
                    }
                ]
            }
        ]
    }
]
伊谢尔伦
let obj1 = {};
let obj={}
let obj2 = eval('(' + (JSON.stringify(obj) + JSON.stringify(obj1)).replace(/}{/, ',') + ')');

It’s very useful, but the browser may prompt that eval is harmful. If you want to remove this, Baidu has a method. I forgot the details

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template