如何添加嵌套数组的属性

时间:2015-07-27 20:22:30

标签: javascript angularjs lodash

我的逻辑在这里没什么问题。

我有手风琴,我拍了一张照片

enter image description here

标题LIVE BETTING,MLB,MLB道具和NBA都在HTML中(实际上是近25项的体育列表)

  <accordion>
    <accordion-group ng-repeat="sport in sports">
      <accordion-heading>
        <div ng-click="addSportToLines(sport);">
          <span>{{sport.name}}</span>
          <span class="badge">{{sport.leagues.length}}</span>
        </div>
      </accordion-heading>

      <div>
        <a ng-repeat="league in sport.leagues"
           ng-click="addLeagueToLines(league)">
           <span>{{league.name}}</span>
           <span class="badge">{{league.offeringsAvailable}}</span>
        </a>
      </div>
    </accordion-group>
  </accordion>

我需要删除包含<span class=badge>的第一个{{sport.leagues.length}},并将{{league.offeringsAvailable}}的总和放在那里。 {{league.offeringsAvailable}}是灰色badge中的数字,正如您在MLB中看到的那样:72,30,30,8,30

所以在黄色徽章中,而不是sport.leagues.length我需要league.offeringsAvailable

的总和

这是sports

的控制器
SportsFactory.getSportsWithLeagues().then(function(sports) {
  $scope.sports = sports;
  console.log(angular.toJson($scope.sports, 'pretty'));
});

这个console.log(angular.toJson($scope.sports, 'pretty'));我得到了这个json

如你所见,标题,例如:LIVE BETTING是第一个,包含一个数组leagues,其中有一个名为offeringsAvailable的道具,所以我需要采用每一个offeringsAvailable }让我们说LIVE BETTING,并将总金额放在$scope变量中,以便在DOM中显示它。

[
  {
    "id": 26,
    "name": "LIVE BETTING",
    "priority": 0,
    "leagues": [
      {
        "id": "3042",
        "composedId": "3042G",
        "name": "NBA - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 3,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3042",
        "composedId": "3044Q",
        "name": "NBA - Live (2Q)",
        "lineType": "Q",
        "part": 2,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3042",
        "composedId": "3045Q",
        "name": "NBA - Live (3Q)",
        "lineType": "Q",
        "part": 3,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3042",
        "composedId": "3046Q",
        "name": "NBA - Live (4Q)",
        "lineType": "Q",
        "part": 4,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3068",
        "composedId": "3068G",
        "name": "Soccer - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 41,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3295",
        "composedId": "3295G",
        "name": "MLB - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 19,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3314",
        "composedId": "3314G",
        "name": "Tennis - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 1,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6250",
        "composedId": "6250G",
        "name": "NHL - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 4,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6250",
        "composedId": "6252P",
        "name": "NHL - Live (2P)",
        "lineType": "P",
        "part": 2,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6250",
        "composedId": "6253P",
        "name": "NHL - Live (3P)",
        "lineType": "P",
        "part": 3,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6711",
        "composedId": "6711G",
        "name": "NCAA Baseball - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 1,
        "sport": {
          "id": 26
        }
      }
    ],
    "img": "images/icons/icon_live_betting.svg"
  },
  {
    "id": 6,
    "name": "MLB",
    "priority": 1,
    "leagues": [
      {
        "id": "1496",
        "composedId": "1496G",
        "name": "MLB",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 72,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "1496",
        "composedId": "1497I",
        "name": "MLB (1I)",
        "lineType": "I",
        "part": 1,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "1496",
        "composedId": "1498I",
        "name": "MLB (2I)",
        "lineType": "I",
        "part": 2,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "1721",
          "id": 6
        }
      },
      {
        "id": "2012",
        "composedId": "2012T",
        "name": "MLB - Alternate Runlines",
        "lineType": "T",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  {
    "id": 41,
    "name": "MLB  Props",
    "priority": 2,
    "leagues": [
      {
        "id": "6726",
        "composedId": "6726G",
        "name": "MLB Props - Runs+Hits+Error",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6727",
        "composedId": "6727G",
        "name": "MLB Props - Team to score 1st",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6728",
        "composedId": "6728G",
        "name": "MLB Props - 1st inning run?",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6731",
        "composedId": "6731G",
        "name": "MLB Props - 1st Called pitch",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6732",
        "composedId": "6732G",
        "name": "MLB Props - Players Matchups",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6741",
        "composedId": "6741G",
        "name": "MLB Props - 1st inning line",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  ... "composedId": "1721T",
        "name": "MLB - Grand Salami",
        "lineType": "T",
        "part": 0,
        "offeringsAvailable": 8,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "2012",
        "composedId": "2012T",
        "name": "MLB - Alternate Runlines",
        "lineType": "T",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  {
    "id": 41,
    "name": "MLB  Props",
    "priority": 2,
    "leagues": [
      {
        "id": "6726",
        "composedId": "6726G",
        "name": "MLB Props - Runs+Hits+Error",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6727",
        "composedId": "6727G",
        "name": "MLB Props - Team to score 1st",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6728",
        "composedId": "6728G",
        "name": "MLB Props - 1st inning run?",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6731",
        "composedId": "6731G",
        "name": "MLB Props - 1st Called pitch",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6732",
        "composedId": "6732G",
        "name": "MLB Props - Players Matchups",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6741",
        "composedId": "6741G",
        "name": "MLB Props - 1st inning line",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  ...

我正在使用LODASH来迭代数组。但我不知道如何完成这项任务

修改

不要混淆,因为我在上面提到了LIVE BETTING,我的意思是,我希望阵列中的每项运动都能发生同样的情况。

2 个答案:

答案 0 :(得分:3)

尝试:

FuncTest2 <- function(pp) {
    EnvTest$Orig <- ifelse(R[,pp]==0|mat[,pp]!=0,EnvTest$Orig,EnvTest$Orig-(P[,pp]-EnvTest$Orig*R[,pp]/1200));
    return(EnvTest$Orig);
};
EnvTest$Orig <- 548453.5;
Test2 <- rbind(EnvTest$Orig,do.call(rbind,lapply(1:9,FuncTest2)));
x2 <- t(Test2);
x2;
##          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]    [,10]
## [1,] 548453.5 542942.6 537422.2 531892.4 526574.8 521245.9 515905.7 510726.1 505533.7 500328.6
## [2,] 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 543171.1 538058.5 532933.3 527795.6
## [3,] 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5

答案 1 :(得分:0)

建议您在收到数据时循环播放每项运动并添加包含总和的offerings属性

sports.forEach(function(sport){ 
    sport.offerings=sport.leagues.reduce(function(prev,curr){     
        return prev + (curr.offeringsAvailable||0);
    },0);
});

然后在标题中使用{{sport.offerings}}