【问题标题】:How to push array in to array?如何将数组推入数组?
【发布时间】:2020-12-02 05:28:24
【问题描述】:

您好,我正在研究 AngularJS 并尝试将数组推入数组: 这是我尝试过的:

$scope.addPositionInUpdateEditor = function (index) {
      $scope.updatePlayersPositionsArray[index].push('PF');
    }

我收到错误无法读取未定义的属性“push”。 $scope.updatePlayersPositionsArray 是这种类型的数组:[0:Array(1)[0: "QB"],1: {}] 我想将“PF”值推送到数组结果中的第一个数组应该如下所示:

[0:Array(2)[0: "QB", 1: "PF"],1: {}]

我该怎么做?

【问题讨论】:

  • index 的值是多少?如果它不是 0,则会出现该错误。 (如果你想推送到第一个元素,为什么要使用参数呢?)
  • 谢谢,索引错误

标签: javascript arrays angularjs


【解决方案1】:

我认为您可以使用concat 方法。正如docs 所说:

concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

您还可以使用其他选项,如下所示:

  • push — 将元素添加到数组的末尾。见here
  • unshift — 将元素添加到数组的开头。见here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2014-06-21
    相关资源
    最近更新 更多