【问题标题】:Angular Js : Scope ArraysAngular Js:范围数组
【发布时间】:2016-05-30 06:41:38
【问题描述】:

HTML

<div ng-controller="MyCtrl">
  this is 'a' array
<br>
  <div ng-repeat ="element in a">
      {{element.value}}
  </div>
<br>
   this is 'b' array
<br>
  <div ng-repeat ="element in b">
      {{element.value}}
  </div>
<br>
  this is final array 
<br>
   <div ng-repeat ="element in final_array">
      {{element.value}}
  </div>
</div>'

js 控制器

var myApp = angular.module('myApp', []);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {

    $scope.a = [{
        "id": 1,
        "value": 10
    }, {
        "id": 2,
        "value": 20
    }]
    $scope.b = [{
        "id": 1,
        "value": 20
    }, {
        "id": 3,
        "value": 40
    }]
    var c = [];
    c = $scope.a.concat($scope.b)
    console.log(c)
    $scope.final_array = [];
    for (var i = 0; i < c.length; i++) {
        if ($scope.final_array.length == 0) {
            $scope.final_array.push(c[i]);

        } else {
            alert("hi")
            for (var j = 0; j < $scope.final_array.length; j++) {
                $scope.flag = 0;
                if ($scope.final_array[j].id == c[i].id) {
                    $scope.final_array[j].value = parseFloat($scope.final_array[j].value) + parseFloat(c[i].value);
                    $scope.flag = 0;
                    break;
                } else {
                    $scope.flag = 1;
                }
            }
            if ($scope.flag == 1) {
                $scope.final_array.push(c[i]);
            }
        }
    }
    console.log($scope.a)
    console.log($scope.b)

    console.log($scope.final_array)
}

我正在连接两个数组 ab 如果键 id 的值相同,我会更新该值。 但是当我在合并数组$scope.final array 上更新时,它会更新数组的值。

这是小提琴JS Fiddle Demo

【问题讨论】:

    标签: javascript html arrays angularjs


    【解决方案1】:

    因为两个数组都指向同一个对象,所以当对象更新时,两个数组都会有更新后的值。

    你可以在将值复制到最终数组时复制原始对象来修复它

    $scope.final_array.push(angular.copy(c[i]));
    

    你可以像这样简化计算逻辑

    var app = angular.module('my-app', [], function() {})
    
    app.controller('AppController', function($scope) {
      $scope.a = [{
        "id": 1,
        "value": 10
      }, {
        "id": 2,
        "value": 20
      }]
      $scope.b = [{
        "id": 1,
        "value": 20
      }, {
        "id": 3,
        "value": 40
      }]
    
      var c = $scope.a.concat($scope.b),
        map = {};
    
      $scope.final_array = [];
      c.forEach(function(item) {
        if (!map[item.id]) {
          map[item.id] = angular.copy(item);
          $scope.final_array.push(map[item.id]);
        } else {
          map[item.id].value += +item.value;
        }
      });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="my-app">
      <div ng-controller="AppController">
        <pre>{{a}}</pre>
        <pre>{{b}}</pre>
        <pre>{{final_array}}</pre>
      </div>
    </div>

    【讨论】:

    • 谢谢 .. 在连接并将 obj 推入数组时应用 angular.copy 对我有帮助 .. 我会在未来的函数中遵循您的计算方法 ..
    猜你喜欢
    • 2014-06-07
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多