How merge two objects array in angularjs?

前端 未结 4 1416
执念已碎
执念已碎 2021-02-03 22:53

I want to append following object array with existing one in angulajs for implementing load more feature.

ie,appending AJAX response with existing one each time.

<
相关标签:
4条回答
  • 2021-02-03 23:06

    Simple

    var a=[{a:4}], b=[{b:5}]
    
    angular.merge(a,b) // [{a:4, b:5}]
    

    Tested on angular 1.4.1

    0 讨论(0)
  • 2021-02-03 23:07

    This works for me :

    $scope.array1 = $scope.array1.concat(array2)
    

    In your case it would be :

    $scope.actions.data = $scope.actions.data.concat(data)
    
    0 讨论(0)
  • 2021-02-03 23:26

    You can use angular.extend(dest, src1, src2,...);

    In your case it would be :

    angular.extend($scope.actions.data, data);
    

    See documentation here :

    https://docs.angularjs.org/api/ng/function/angular.extend

    Otherwise, if you only get new values from the server, you can do the following

    for (var i=0; i<data.length; i++){
        $scope.actions.data.push(data[i]);
    }
    
    0 讨论(0)
  • 2021-02-03 23:30
    $scope.actions.data.concat is not a function 
    

    same problem with me but i solve the problem by

     $scope.actions.data = [].concat($scope.actions.data , data)
    
    0 讨论(0)
提交回复
热议问题