将两个对象与数组合并在一起

我试图合并两个JavaScript对象,其中有数组和其他元素。 让我举一个例子。

{ "addresses": [ { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai1@gmail.com" }, { "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839332" }, { "type": "email", "tags": [ "Mãe" ], "address": "johndoemae1@gmail.com" }, { "type": "email", "tags": [ "Aluno" ], "address": "johndoealuno1@gmail.com" } ], "class": [ "Sala 1", "Sala 2", "Sala 3" ], "fullname": "John Doe 1", "eid": "2", "invisible": true, "see_all": false }, { "addresses": [ { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai2@gmail.com" }, { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai3@gmail.com" }, { "type": "phone", "tags": [ "Pai" ], "address": "5519985504400" }, { "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839333" }, { "type": "email", "tags": [ "Mãe" ], "address": "11 983340440" }, { "type": "email", "tags": [ "Aluno" ], "address": "" } ], "class": [ "Sala 4", "Sala 5", "Sala 6" ], "fullname": "John Doe 1", "eid": "2", "invisible": false, "see_all": true } 

正如你所看到的,我们可以在这些数组中使用一些string。 我试图用一些recursion来做,但没有成功。

 function mergeObjects(target, source){ var item, tItem, o, idx; if (typeof source == 'undefined') { return source; } else if (typeof target == 'undefined') { return target; } for (var prop in source) { item = source[prop]; //check if the first element is indeed an object. if (typeof item == 'object' && item !== null) { //if the first item is an array if (_.isArray(item) && item.length) { //dealing with array of primitives if (typeof item[0] != 'object') { item.forEach(function(conteudo){ //push to the target all the elements; target[prop].push(conteudo); }); }else{ //dealing with array of objects; for(var attr in item){ idx = {}; tItem = target[attr] mergeObjects(tItem,item); } } }//if its a normal object else { // deal with object mergeObjects(target[prop],item); } } else { // item is a primitive, just copy it over target[prop] = item; } } return target; } 

我期待这一点:

  { "addresses": [ { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai2@gmail.com" }, { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai3@gmail.com" }, { "type": "phone", "tags": [ "Pai" ], "address": "5519985504400" }, { "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839333" }, { "type": "email", "tags": [ "Mãe" ], "address": "11 983340440" }, { "type": "email", "tags": [ "Aluno" ], "address": "" } ], "class": [ "Sala 4", "Sala 5", "Sala 6", "Sala 1", "Sala 2", "Sala 3" ], "fullname": "John Doe 1", "eid": "2", "invisible": true, "see_all": false } "fullname": "John Doe 1", "eid": "1234", "classes": [ "Sala 1", "Sala 2", "Sala 3", "Sala 4", "Sala 5", "Sala 6" ], "addresses": [{ "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839332" }, { "type": "email", "tags": [ "Mãe" ], "address": "johndoemae1@gmail.com" }, { "type": "email", "tags": [ "Aluno" ], "address": "johndoealuno1@gmail.com" }, { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai2@gmail.com" }, { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai3@gmail.com" }, { "type": "phone", "tags": [ "Pai" ], "address": "5519985504400" }, { "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839333" }], "invisible": true, "see_all": true } 

但是我得到的是这个,正如你所看到的,一些电子邮件元素丢失了。

 { "addresses": [ { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai2@gmail.com" }, { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai3@gmail.com" }, { "type": "phone", "tags": [ "Pai" ], "address": "5519985504400" }, { "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839333" }, { "type": "email", "tags": [ "Mãe" ], "address": "11 983340440" }, { "type": "email", "tags": [ "Aluno" ], "address": "" } ], "class": [ "Sala 4", "Sala 5", "Sala 6", "Sala 1", "Sala 2", "Sala 3" ], "fullname": "John Doe 1", "eid": "2", "invisible": true, "see_all": false } 

元素顺序不是问题。
我错过了recursion树的哪一点?

简短的回答:你必须先自己合并arrays。 或者你可以使用Lodash的mergeWidth Ramda 的R.mergeWith (请参阅下面的Ramda示例代码)。

你想要的东西像本地Object.assign(有限的浏览器支持,所以它将不得不polyfilled)或任何合并你的JS库。

您遇到的问题是所有这些合并实现如何处理重复键。 通常情况下,密钥将被设置为传入的最后一个值:

 var thing1 = { someProperty: 'foo' }; var thing2 = { someProperty: 'bar' }; var result1 = Object.assign({}, thing1, thing2); // -> {someProperty: 'bar'} (set to the last value passed in, in thing2) var result2 = Object.assign({}, thing2, thing1); // -> {someProperty: 'foo'} (again set to the last value passed in, in thing1) // Make the Stackoverflow snippet display the output. document.body.innerHTML += JSON.stringify(result1, null, 2); document.body.innerHTML += '<br>' + JSON.stringify(result2, null, 2);