1

I have 2 separate arrays which I need to merge into a third one so I can get all the data required. Basically the 1st array has an id, and name and in order to get the address I need to search inside the 2nd array and match the id's so I can have all the data from the person.

Here is the data and code:

//Array 1
var myPeopleArray = [{"people":[{"id":"123","name":"name 1"},{"id":"456","name":"name 2"}]}];

//Array 2
var myPersonArray = [{"person":[{"id":"123","address":"address 1"},{"id":"456","address":"address 2"}]}];

    var arrayLength = myPeopleArray[0].people.length;

    for (var i = 0; i < arrayLength; i++) {

        console.log("id: " + myPeopleArray[0].people[i].id);

    }

//Wanted Result: 

[{"people":[

    {
        "id":"123",
        "name":"name 1",
        "address":"address 1"
    },

    {
        "id":"456",
        "name":"name 2",
        "address":"address 2"
    }
]

}]

How can I do this?

  • Did you do any googling? http://stackoverflow.com/questions/13514121/merging-two-collections-using-underscore-js . If you can't use underscore there are also results out there that will help you . – Nix Sep 21 '16 at 10:50
  • Why are you - or why is your script - creating the two arrays in the first place? – David Thomas Sep 21 '16 at 10:51

2 Answers2

1
var myPeopleArray = [{"people":[{"id":"123","name":"name 1"},    {"id":"456","name":"name 2"}]}];
var myPersonArray = [{"person":[{"id":"123","address":"address 1"},   {"id":"456","address":"address 2"}]}];

for(var i=0;i<myPeopleArray[0].people.length;i++)
 {
myPeopleArray[0].people[i].address =  myPersonArray[0].person[i].address;
} 
document.write(JSON.stringify(myPeopleArray));
halim
  • 211
  • 1
  • 10
0

You could iterate both arrays and build new object with the joined properties.

var myPeopleArray = [{ "people": [{ "id": "123", "name": "name 1" }, { "id": "456", "name": "name 2" }] }],
    myPersonArray = [{ "person": [{ "id": "123", "address": "address 1" }, { "id": "456", "address": "address 2" }] }],
    hash = Object.create(null),
    joined = [],
    joinById = function (o) {
        if (!(o.id in hash)) {
            hash[o.id] = {};
            joined.push(hash[o.id]);
        }
        Object.keys(o).forEach(function (k) {
            hash[o.id][k] = o[k];
        });
    };

myPeopleArray[0].people.forEach(joinById);
myPersonArray[0].person.forEach(joinById);

console.log(joined);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • What about if the id field on myPersonArray has a different name? –  Sep 21 '16 at 11:57
  • what do you want in this case? just overwrite, as above or preserve the old name? – Nina Scholz Sep 21 '16 at 12:00
  • Basically, at the moment you are matching id with id by I need to do one where the field name on people array is ID and the matching field on Person array is another name ...What do I have to change in the code for this? –  Sep 21 '16 at 12:04
  • what do you want to get? please add an example and the wanted result. – Nina Scholz Sep 21 '16 at 12:08