通过值为数组的键组合两个对象

时间:2017-02-01 13:16:54

标签: javascript lodash

我有两个javascript对象,如下面的

first =  {b : ["A" , "B"] , c : ["C" , "D"] , d : ["Z"]}
second = {b : ["E" , "F"] , c : ["X"]}

我想要这样的结果

result = {b : ["A" , "B" , "E" , "F"] , c : ["C" , "D" ,"X"] , d :["Z"]}

我正在玩npm lodash,但找不到办法。一些身体可以帮助。我是Javascript世界的新手。

5 个答案:

答案 0 :(得分:3)

您可以使用mergeWith()并将自定义函数添加到concat数组作为customizer参数。



var first =  {b : ["A" , "B"] , c : ["C" , "D"] , d : ["Z"]}
var second = {b : ["E" , "F"] , c : ["X"]}

var result = _.mergeWith(first, second, function(a, b) {
  return [].concat(a, b)
})
console.log(JSON.stringify(result))

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这会对你有帮助。

&#13;
&#13;
var first =  {b : ["A" , "B"] , c : ["C" , "D"] , d : ["Z"]};
var second = {b : ["E" , "F"] , c : ["X"]};

var newObj = {};

for(let i in first) {
  if(second.hasOwnProperty(i)) {
    var tempArray = first[i];
    
    for(let j in second[i]) {
      tempArray.push(second[i][j]);
    }
    
    newObj[i] = tempArray;
    
  } else {
    newObj[i] = first[i];
  }
}


console.log(JSON.stringify(newObj));
&#13;
&#13;
&#13;

答案 2 :(得分:0)

var first = {b : ["A" , "B"] , c : ["C" , "D"] , d : ["Z"]};
var second = {b : ["E" , "F"] , c : ["X"]};
// merge expects an array of objects to be used as its parameter
var merge = function merge( objs ) {
    var result = {};
    // for each object sent to this function.
    objs.forEach(function( obj ) {
        // Grab the array containing all the property keys.
        var keys = Object.keys(obj);
        // Loop over each keyname
        keys.forEach(function( keyName ) {
            // first, check if the key exists in our result. If not, create it and assign it an array.
            if (!result.hasOwnProperty(keyName)) result[keyName] = [];
            // Merge the array inside the object into the result by concatenation.
            result[keyName] = result[keyName].concat(obj[keyName]);
        });
    });
    return result;
};
// Use the function
var result = merge([ first, second ]);
console.log(JSON.stringify(result));
// The caveat here is that all the letters in the array are just added, we don't check for duplicates atm
// If duplicates are a thing, just change the .concat into another loop that checks if a letter is already in the array or not.

答案 3 :(得分:0)

lodash mergeWith docs使用这个确切的东西作为例子:

function customizer(objValue, srcValue) {
  // if merging 2 arrays, concat them together
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

var first =  {b : ["A" , "B"] , c : ["C" , "D"] , d : ["Z"]},
second = {b : ["E" , "F"] , c : ["X"]}
console.log(_.mergeWith(first, second, customizer));
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.js"></script>

答案 4 :(得分:0)

由于不需要第三方库(lodash或其他)和一个不错的功能风格,你可以这样做

let first =  {b : ["A" , "B"] , c : ["C" , "D"] , d : ["Z"]}
let second = {b : ["E" , "F"] , c : ["X"]}
let sources = [first, second];

let unique = (...sources) => Array.from(new Set([].concat(...sources)));
let join = (...sources) => key => [].concat(...sources.map(source => source[key] || []));

let keys = unique(...sources.map(source => Object.keys(source)));

let merged = keys.reduce((map, key) => (map[key] = join(first, second)(key)) && map, {});

console.log(merged);

这将处理任意数量的字典作为源并将它们合并在一起。

相关问题