Merge array object algorithm

data A:

[
    {
        "id":340,
        "name":"",
        "sub":[
            {
                "id":341,
                "name":""
            },
            {
                "id":367,
                "name":"",
                "children":[
                    {
                        "id":427,
                        "name":""
                    }
                ]
            }
        ]
    }
]

data B:

[
    {
        "id":340,
        "name":"",
        "sub":[
            {
                "id":342,
                "name":""
            },
            {
                "id":367,
                "name":"",
                "children":[
                    {
                        "id":428,
                        "name":""
                    }
                ]
            }
        ]
    },
    {
        "id":344,
        "name":"/",
        "sub":[
            {
                "id":347,
                "name":""
            },
            {
                "id":363,
                "name":"",
                "children":[
                    {
                        "id":421,
                        "name":""
                    }
                ]
            }
        ]
    }
]

data B is merged into data A result:

[
    {
        "id":340,
        "name":"",
        "sub":[
            {
                "id":341,
                "name":""
            },
            {
                "id":367,
                "name":"",
                "children":[
                    {
                        "id":427,
                        "name":""
                    },
                    {
                        "id":428,
                        "name":""
                    }
                ]
            },
            {
                "id":342,
                "name":""
            }
        ]
    },
    {
        "id":344,
        "name":"/",
        "sub":[
            {
                "id":347,
                "name":""
            },
            {
                "id":363,
                "name":"",
                "children":[
                    {
                        "id":421,
                        "name":""
                    }
                ]
            }
        ]
    }
]

function merge (arr1, arr2, level = 0) {
    let result = []
    let idToValue = {}
    let keys = arr2.map(value => {
        idToValue[value.id] = {
            added: false,
            value
        }
        return value.id
    })

    let childKey = level === 0 ? 'sub' : 'children'
    arr1.forEach(cur => {
        let value = Object.assign({}, cur)
        result.push(value)
        let tmp = idToValue[cur.id]
        if (tmp) {
            tmp.added = true
            let child1 = cur[childKey] || []
            let child2 = tmp.value[childKey] || []
            if (child1.length || child2.length) {
                value[childKey] = merge(child1, child2, PPlevel)
            }
        }
    })
    keys.forEach(cur => {
        if (!idToValue[cur].added) {
            result.push(Object.assign({}, idToValue[cur].value))
        }
    })
    return result
}
merge(arr1, arr2)

use Lodash's mergeWith to write a custom merge function to handle arrays

function customMerge(dest, src, key) {
    if (_.isArray(dest)) {
        src.forEach(sourceModel => {
            const destModel = dest.find(m => m.id === sourceModel.id);
            if (destModel) {
                _.mergeWith(destModel, sourceModel, customMerge);
            } else {
                dest.push(sourceModel);
            }
        });
        return dest;
    }
}

const c = _.mergeWith(a, b, customMerge);
console.log(c);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35e31-2bfd8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35e31-2bfd8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?