【问题标题】:Merge two arrays into one with push()使用 push() 将两个数组合并为一个
【发布时间】:2016-06-17 11:43:41
【问题描述】:

我有以下代码:

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

我希望得到以下结果:

列表1:

  • 0:对象(苏黎世)
  • 1:对象(伦敦)
  • 3:对象(纽约)
  • 4:对象(虚拟)
  • 5:对象(Dummy2)

但我得到了这个:

列表1:

  • 0:对象(苏黎世)
  • 1:对象(伦敦)
  • 2:对象(纽约)
  • 3:对象(项目)
    • 0:对象(虚拟)
    • 1:对象(虚拟 2)

我怎样才能得到我的预期结果?

感谢和欢呼。

【问题讨论】:

  • 您提供的代码list1.push(list2); 将失败并出现TypeError。为什么你期望4: Object (Items) 而不是4: Object (Dummy)
  • @squint 那是一个错字...而且我没有遇到错误,它会打印我的第二个结果以防我的预期..但是使用 concat 它可以工作..
  • list1 没有 .push() 方法,所以你必须在做其他事情。 .push().concat() 方法非常不同。一个突变,另一个替换。在某些情况下,这可能是一个重要的区别。
  • 关于.apply()的问题,您需要将list1.Items设置为.push()的“this”值,这样您就可以将list2.Items的成员作为单独的args传递。你可以这样做:Array.prototype.push.apply(list1.Items, list2.Items)list1.Items.push.apply(list1.Items, list2.Items)。那些有效地使电话最终像您一样:list1.Items.push(list2.Items[0], list2.Items[1], ...and so on... )
  • 是的,就是这样,只不过@9​​87654336@构造了一个新的Array只是为了访问.push(),这看起来很浪费。您已经有一个带有list1.Items 的数组,或者您可以使用Array.prototype.push 获取该方法。

标签: javascript jquery object push


【解决方案1】:

除了Array#concat,你还可以使用Array#push.apply

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

【讨论】:

  • 您好,谢谢,这行得通。申请呢?我的意思是:list1.push.apply(list2)?我听说过一些关于它的事情,但我不知道它是如何工作的......你知道吗?干杯,谢谢。
  • 嗨,我遇到了以下错误:无法读取未定义的属性“应用”?
  • 请在你的答案中删除 concat 解决方案,我用 push/push 搜索了一个,申请.. 谢谢
【解决方案2】:

问题是如何使用 push() 而不是 concat():

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

【讨论】:

  • 好的先生,谢谢!我有一个问题:是否还有一种聪明的方法可以在没有循环但仍使用推送的情况下合并此列表?我听说过一些关于 push.apply() 的事情,但我不知道它是如何工作的或如何使用它。干杯。。
  • @MrBuggy 是的。您可以使用 Array.prototype.push.apply(list1.Items, list2.Items); - 这作为 apply 的第一个参数将 this 的值设置为 list1.Items,第二个参数是要传递给 push 的参数数组。
  • 您好,谢谢!在标记为正确的答案中,您可以看到我使用应用搜索的方式。谢谢和欢呼..
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    使用spread operator

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

    Spread 是 ES2015 的一个特性。您的目标浏览器或运行时可能还不支持它,因此请检查compatibility table(或使用像babel 这样的转译器)。

    【讨论】:

      【解决方案5】:

      尝试:

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

      【讨论】:

        【解决方案6】:

        您需要遍历list2 中的每个items,然后获取它们以推送到list1.. 下面是使用$.each 的sn-p

        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);
        &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

        【讨论】:

          猜你喜欢
          • 2021-07-16
          • 2012-11-19
          • 1970-01-01
          • 2019-08-30
          • 2011-06-02
          • 2022-01-10
          • 2014-05-14
          • 1970-01-01
          相关资源
          最近更新 更多