2016-02-27 72 views
0

我正在使用lodash合并2个对象。因为要合并的第二个对象我不知道它可能包含一个点符号字符串对象。 (不知道是不是一个更好的词吗?)使用lodash合并2个对象,但使用点符号

简单的(工作)例如:

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

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

但现在用点符号的工作:

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

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

这不是预期的结果 - 而且我甚至不知道这应该如何在一个对象中两次使用keys.name.name。

+0

使用它与合并之前,您应该扁平化你的第二个对象键。 – Darshan

回答

0

您在这两个示例中使用的第二个参数不相同。当你想在对象键中使用一个点时,你需要在你的案例中引用键名(person.name)。

因此,您的第一个示例中的对象具有一个键person,该键指向具有name键的对象。相比之下,第二个示例中的对象有一个名为person.name的键,它有些不同。在第二个样品上访问person键将返回undefined

0

一个小帮手

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; 
} 

和使用

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

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