js 两个嵌套对象的合并

2020-09-03  本文已影响0人  蜗牛Coming

话不多说,直接上代码

  export function MergeRecursive(obj1, obj2) {
    let arr = Object.keys(obj2);
    let index = -1;
    while (++index < arr.length) {
        let p = arr[index];
        try {
            if (obj2[p].constructor == Object) {
                obj1[p] = MergeRecursive(obj1[p], obj2[p]);
            } else {
                obj1[p] = obj2[p];
            }

        } catch (e) {
            obj1[p] = obj2[p];
        }
    }
    return obj1;
}
上一篇 下一篇

猜你喜欢

热点阅读