简体   繁体   中英

How to push array in to array?

Hi I am working on AngularJS and try to push array in to array: This is what I tried:

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

I get error Cannot read property 'push' of undefined . $scope.updatePlayersPositionsArray is this type of array: [0:Array(1)[0: "QB"],1: {}] I want to push 'PF' value to first array in array result should look like this:

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

How should I do that?

Try to use the concat method. As the docs says:

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

There is also other options that you can use, those are the following:

  • push — add elements to the end of the array. See here .
  • unshift — add elements to the beginning of the array. See here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM