4

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

2
  • 1
    Jina, you appear to be missing the closing bracket (}) for modifierOuCreerArticle. Commented Apr 20, 2016 at 11:53
  • 1
    your final code should be: $scope.listfinal = $scope.finalOperationsList.concat($scope.listOperationsById); Commented Apr 20, 2016 at 11:57

3 Answers 3

6

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]
Sign up to request clarification or add additional context in comments.

2 Comments

yes Sir,I know that,but my problem is that I can't get the Data from the $scope.listOperationsById function ,how can I modify my code to get this data and merge it with the finalOperationsList array?
try this: $scope.listOperationsById = function(id) { $http.get(URL2) .success(function(data) { $scope.listOperationsById = data; $scope.listfinal = $scope.finalOperationsList.concat(data) }); }
4

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

2 Comments

@andreasonny83 that's not a problem dude :)
thanks Sirs but using the concat method doesn't help me,I get always the data from the "$scope.finalOperationsList"
1

You should use angular's $q.all : https://docs.angularjs.org/api/ng/service/$q

It'll allow you to easily handle all the data, when both of your async methods return

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.