Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge two arrays in angularJS

I have two arrays,I want to merge them into one array to bind the Data to the HTML form,this is what I did:

Controller:

$scope.modifierOuCreerArticle = function() {
    var index = this.row.rowIndex;

    $http.get("URL1")
    .success(function(datagetArticle) {
        $scope.finalOperationsList = datagetArticle.listElementGamme;
        var v = $scope.finalOperationsList[$scope.finalOperationsList.length-1].operationId;


        $scope.listOperationsById(v);
        $scope.listfinal=$scope.finalOperationsList.concat($scope.listOperationsById);

        $scope.finalOperationsList = $scope.listfinal;
    });

$scope.listOperationsById = function(id) {
    $http.get(URL2)
        .success(function(data) {
            $scope.listOperationsById = data;
        });
}

I want to merge the content of "finalOperationsList" array and "listOperationsById" array and send the content to my form with the "listfinal"

but I get this in console:

$scope.listfinal :[{ content of finalOperationsList},null]

so please how can I correct my code to get the all data coming from the merge of "finalOperationsList" and "listOperationsById" arrays thanks for help

like image 293
Jina Avatar asked Dec 19 '22 17:12

Jina


2 Answers

Consider using the concat JavaScript method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

var alpha = ['a', 'b', 'c'],
  numeric = [1, 2, 3];

var alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); // Result: ['a', 'b', 'c', 1, 2, 3]
like image 137
andreasonny83 Avatar answered Jan 06 '23 14:01

andreasonny83


Use conact function

var a = ["1", "2", "3", "4"];
var b = ["5", "6", "7", "8"];
var c = a.concat(b);

OR

angular.extend(c, a, b);
like image 22
byteC0de Avatar answered Jan 06 '23 14:01

byteC0de