最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Merge two arrays into one with push() - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Jun 17, 2016 at 11:56 webta.st.ic asked Jun 17, 2016 at 11:43 webta.st.icwebta.st.ic 5,1798 gold badges54 silver badges107 bronze badges 8
  • The code you provided list1.push(list2); would fail with a TypeError. And why do you expect 4: Object (Items) instead of 4: Object (Dummy)? – user1106925 Commented Jun 17, 2016 at 11:54
  • @squint That was a typo... And I hav'nt got an error, it prints my the second result in case of my expected..But with the concat it works.. – webta.st.ic Commented Jun 17, 2016 at 11:57
  • list1 doesn't have a .push() method, so you must be doing something else. The .push() and .concat() methods are very different. One mutates, the other replaces. This can be an important distinction in some cases. – user1106925 Commented Jun 17, 2016 at 11:58
  • 1 As to your question about .apply(), you need to set list1.Items as the "this" value of .push(), so that you can pass the members of list2.Items as individual args. You do that like this: Array.prototype.push.apply(list1.Items, list2.Items) or this list1.Items.push.apply(list1.Items, list2.Items). Those effectively make the call end up as though you did: list1.Items.push(list2.Items[0], list2.Items[1], ...and so on... ) – user1106925 Commented Jun 17, 2016 at 12:01
  • 1 Yes, that's the same thing, except that the [] constructs a new Array just to access .push(), which seems wasteful. You already have an Array with list1.Items or you can get the method with Array.prototype.push. – user1106925 Commented Jun 17, 2016 at 12:16
 |  Show 3 more ments

6 Answers 6

Reset to default 8

Beside Array#concat, you could use Array#push.apply for it

var 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" }] };

[].push.apply(list1.Items, list2.Items);

console.log(list1);

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]);
}

Use the spread operator:

list1.Items.push(...list2.Items)

Spread is an ES2015 feature. Your target browsers or runtime may not support it yet, so check the patibility table (or use a transpiler like babel).

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.Items = list1.Items.concat(list2.Items);
console.log(list1);

try with:

list2.items.forEach(function (item) {
  list1.items.push(item)
})

You need to loop through each items in list2 and then fetch them to push into list1.. Below is the snippet using $.each

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

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

$(list2.Items).each(function(k,v){
  list1.Items.push(v);
  
})
console.log(list1);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论