深度合并两个JavaScript对象lodash/vanilla [英] Deep Merging two javascript objects lodash/vanilla

查看:51
本文介绍了深度合并两个JavaScript对象lodash/vanilla的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并两个包含对象的对象.这些对象包含一个对象数组.但是,我的代码无法产生理想的结果,而且我也不明白为什么.可能是因为我刚开始使用lodash并缺少明显的东西

I am trying to merge two Objects with containing objects..which contain an array of object..you got the idea. But, my code just not producing the desired result, and I can't understand why..probably because I just starting using lodash and I missing something obvious

这是我要合并的两个对象

Here are two objects which I want to merge

 var original = { 
    a: "bbbb", 
    Id: 1, 
    b: { 
        a: "bbb", 
        Id: 2 
    }, 
    c: "aaa",
    d: [
        { 
            a: "bbbb", 
            Id: 1, 
            b: { 
                a: "bbb", 
                Id: 2 
            }, 
            c: "aaa"
        },
        { 
            a: "bbbb", 
            Id: 2, 
            b: { 
                a: "bbb", 
                Id: 3 
            }, 
            c: "aaa" 
        }
    ] 
};

var source = { 
    a: "aaa", 
    Id: 1, 
    b: { 
        a: "aaa", 
        Id: 2 
    }, 
    c: null, 
    d: [
        { 
            a: "aaa", 
            Id: 1, 
            b: { 
                a: "aaa", 
                Id: 2 
            }, 
            c: null
        },
        { 
            a: "aaa", 
            Id: 2, 
            b: { 
                a: "aaa", 
                Id: 3 
            }, 
            c: null 
        }
    ] 
};

结果对象应仅包含"aaa"作为值,并且不包含任何null.(从"source"获取值,除非它为null,并且不复制仅复制数组,而是将对象合并到其中.)我的代码可以很好地合并对象...但是,当到达对象数组时,它无法产生正确的结果

The result object should contain only "aaa" as values and no nulls.(take value from "source", unless it null and don't copy just copy arrays,but rather merge objects inside of them..) My code merges the objects just fine...but, it fails to produce correct result when it gets to the array of objects

结果应为:

{"a":"aaa",
 "Id":1,
 "b":
    {
     "a":"aaa",
     "Id":2
    },
    "c":"aaa",
   "d":
     [
      {
       "a":"aaa",
       "Id":1,
        "b":
         {
         "a":"aaa",
         "Id":2
         },
        "c":"aaa"
     },

{"a":"aaa","Id":2,"b":{"a":"aaa","Id":3},"c":"aaa"}]}

{ "a":"aaa", "Id":2, "b": { "a":"aaa", "Id":3 }, "c":"aaa" } ]}

这是我的代码: https://jsfiddle.net/yodfcn6e/

谢谢!

推荐答案

仅当目标具有 null 值时,才可以迭代对象的键并进行更新.

You could iterate the keys of the object and update only if the target has null values.

function update(source, target) {
    Object.keys(source).forEach(function (k) {
        if (target[k] === null) {
            target[k] = source[k];
            return;
        }
        if (source[k] && typeof source[k] === 'object') {
            update(source[k], target[k]);
        }
    });
}

var source = { a: "bbbb", Id: 1, b: { a: "bbb", Id: 2 }, c: "aaa", d: [{ a: "bbbb", Id: 1, b: { a: "bbb", Id: 2 }, c: "aaa" }, { a: "bbbb", Id: 2, b: { a: "bbb", Id: 3 }, c: "aaa" }] },
    target = { a: "aaa", Id: 1, b: { a: "aaa", Id: 2 }, c: null, d: [{ a: "aaa", Id: 1, b: { a: "aaa", Id: 2 }, c: null }, { a: "aaa", Id: 2, b: { a: "aaa", Id: 3 }, c: null }] };

update(source, target);

console.log(target);

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

这篇关于深度合并两个JavaScript对象lodash/vanilla的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆