从AngularJS中的嵌套数组进行计算

时间:2017-02-21 15:03:42

标签: javascript angularjs arrays

我有以下数组:

$scope.products = [
  {
    nom : "Pa de fajol",
    img : "dill1.jpg",
    descripcio: [
      "Pa de motlle"
    ],
    preu:4.90,
    tipus: "Pa"
  },
  {
    nom : "Pagès semi integral de blat/espelta",
    img : "dill2.jpg",
    descripcio: [
      "Pa de pagès",
      "680g / 400g"
    ],
    tipus: "Pa",
    variacions : [
      {
        nom: "Gran",
        preu: "3.70",
        grams: "680g",
        basket: 0
      },
      {
        nom: "Petit",
        preu: "2.30",
        grams: "400g",
        basket: 1
      }
    ]
  }

在前端,我用ng-repeat显示它,并且根据产品是否有价格(preu),我显示单个产品的数量输入,或每个变化的一个输入。

看起来像这样:

<div ng-repeat="pa in products">
  <div ng-if="!pa.preu" ng-repeat="vari in pa.variacions">
    <input type="number" ng-model="pa.variacions.basket" ng-init="pa.variacions.basket=0">
  </div>
  <input ng-if="pa.preu" type="number" ng-model="pa.basket" name="basket" ng-init="pa.basket=0">
</div>
<a ng-click="insert()">Add to cart</a>

当我触发insert()时,我有一个功能可以添加产品并计算总价格,但到目前为止它只适用于有价格的产品,而不适用于变化。

该功能如下所示:

$scope.insert = function() {
  $scope.singleorder = [];

  for(var i = 0; i < $scope.products.length; i++)
    if($scope.products[i].basket) { // if input has been setted
      $scope.singleorder.push({
        'product': $scope.products[i].nom,
        'number': $scope.products[i].basket,
        'preu': ($scope.products[i].preu * $scope.products[i].basket)
      });
    }
  console.log($scope.singleorder);
}

我如何继续在$scope.singleorder数组中包含来自嵌套变体数组的计算?

任何提示?

修改

请参阅Plunkr以重现问题

我设法在一些评论的帮助下解决了这个问题!

请参阅Plunkr获取最终版本。 这是最后的功能:

$scope.insert = function() {
  $scope.singleorder = [];
  for(var i = 0; i < $scope.products.length; i++)
    if($scope.products[i].basket) { // if input has been setted
      $scope.singleorder.push({
        'product': $scope.products[i].nom,
        'number': $scope.products[i].basket,
        'preu': ($scope.products[i].preu * $scope.products[i].basket)
      });
    }
    else if($scope.products[i].variacions) { // if input has been setted
      angular.forEach($scope.products[i].variacions, function(variacions) {
        if(variacions.basket) {
          $scope.variname =  $scope.products[i].nom + ' (' + variacions.nom + ')'  
          $scope.singleorder.push({
            'product': $scope.variname,
            'number': variacions.basket,
            'preu': (variacions.preu * variacions.basket)
          });
        }
      });
    }

  console.log($scope.singleorder);

1 个答案:

答案 0 :(得分:0)

你的问题是你的 variacions 是一个使用它的数组,

$scope.insert = function() {
      $scope.singleorder = [];
      for(var i = 0; i < $scope.products.length; i++)
        if($scope.products[i].basket) { // if input has been setted
          $scope.singleorder.push({
            'product': $scope.products[i].nom,
            'number': $scope.products[i].basket,
            'preu': ($scope.products[i].preu * $scope.products[i].basket)
          });
        }
        else if($scope.products[i].variacions) { // if input has been setted
          for(var j=0;j<$scope.products[i].variacions.length; j++){
            $scope.singleorder.push({
            'product': $scope.products[i].variacions[j].nom,
            'number': $scope.products[i].variacions[j].basket,
            'preu': ($scope.products[i].variacions[j].preu * $scope.products[i].variacions[j].basket)
          });
          }
        }

      console.log($scope.singleorder);

    }

请参阅Plunkr