简体   繁体   English

使用lodash合并2个对象,但点符号

[英]Use lodash to merge 2 objects but with dot notation

I am using to use lodash to merge 2 objects. 我正在使用lodash合并2个对象。 Because the second object to merge I dont know it is possible that it will contain a dot-notation-string object. 因为第二个要合并的对象,我不知道它可能包含一个点符号字符串对象。 (Do not know a better word for it ?) (不知道一个更好的词吗?)

Simple (working) example: 简单(有效)示例:

_.merge({person:{name: 'Marc', age: 28}}, {person:{name: 'Timo'}});

// This will return {person:{name: 'Timo', age: 28}}

But now working with a dot notation: 但是现在使用点表示法:

_.merge({person:{name: 'Marc', age: 28}}, {'person.name': 'Timo'});

// This will return {person:{name: 'Marc', age: 28}, person.name: 'Timo'}

This is not the expected result - and I even dont know how this should work with the keys person.name twice in one object. 这不是预期的结果-我什至不知道如何在一个对象中两次使用person.name键。

The second arguments you used in the two samples are not identical. 您在两个示例中使用的第二个参数不相同。 When you want to use a dot inside of an object key, you need to quote the key name ( person.name ) in your case. 如果要在对象键的内部使用圆点,则需要使用键名( person.name )进行引用。

So the object in your first sample has a key person which points to an object having a name key. 因此,第一个样本中的对象有一个关键人物,该person指向具有name关键字的对象。 In contrast, the object in your second sample has a key named person.name which is something different. 相比之下,第二个示例中的对象具有一个名为person.name的键,该键有所不同。 Accessing the person key on the second sample will return undefined . 访问第二个示例上的person键将返回undefined

a little helper 小帮手

function setPath(obj, path, value){
    if(typeof path === "object"){
        //you might want to change this part to lodash
        return Object.keys(path)
            //sort ASC by key-length
            //to make sure that the key `person` would be processed 
            //before processing `person.name`
            .sort((a,b)=>a.length-b.length)
            //apply keys
            .reduce((o, k) => setPath(o, k, path[k]), obj);
    }

    var parts = String(path).split(".");
    for(var i = 0, last = parts.length-1, ctx = obj; i<last; ++i, ctx = v){
        var k = parts[i], v = ctx[k];
        if(v !== Object(v)){
            //you might want to throw an error, or to ignore these cases
            //it's up to you
            if(v != null) console.error("overwriting non null property at path: " + parts.slice(0, i+1).join("."));

      //simple
            v = ctx[k] = {};

            /*
            //check the next key, if it is an uint, 
            //then this should probably be an Array
            var w = parts[i+1];
            //check wether w contains an uint32 
            v = ctx[k] = (+w === (w>>>0))? []: {};
            */
        }
    }
    ctx[parts[last]] = value;

    return obj;
}

and the usage 和用法

var a = { person: { name: "Marc", age: 28 } };
var b = { "person.name": "Timo" };

JSON.stringify(setPath(a, b), null, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM