BeeNag BeeNag - 7 months ago 38
Javascript Question

Merge objects in arrays

I have two arrays:

[{Name: "Jack", Depot: "5"}, {Name: "Jill", Depot: "6"}]


[{Depot Name: "Edgware"}, {Depot Name: "Romford"}]

I need to take the objects from the second array and merge them with the objects in the first array to produce a result of:

[{Name: "Jack", Depot: "5", Depot Name: "Edgware"}, {Name: "Jill", Depot: "6", Depot Name: "Romford"}]

Any help with this would be much appreciated


var array1 = [{
  Name: "Jack",
  Depot: "5"
}, {
  Name: "Jill",
  Depot: "6"
var array2 = [{
  'Depot Name': "Edgware"
}, {
  'Depot Name': "Romford"

for (var a in array1) {
  for (var p in array1[a]) {
    array2[a][p] = array1[a][p];