MrBuggy MrBuggy - 5 months ago 17
Javascript Question

Merge two arrays into one with push()

I've got follow code:

list1 = {
Items: [
{
ID: 1,
Name: "Zurich"
},
{
ID: 2,
Name: "London"
}, {
ID: 3,
Name: "New York"
}
]
};

list2 = {
Items: [
{
ID: -1,
Name: "Dummy"
},
{
ID: 0,
Name: "Dummy2"
}
]
};

list1.push(list2);


I expect follow result:

list1:



  • 0: Object (Zurich)

  • 1: Object (London)

  • 3: Object (New York)

  • 4: Object (Dummy)

  • 5: Object (Dummy2)




But I get this one:

list1:


  • 0: Object (Zurich)

  • 1: Object (London)

  • 2: Object (New York)

  • 3: Object (Items)


    • 0: Object (Dummy)

    • 1: Object (Dummy2)




How can I get my expectet result?

Thanks and cheers.

Answer

The question was how to do this with push() not concat():

for (var i = 0; i < list2.Items.length; i++) {
    list1.Items.push(list2.Items[i]);
}