gpt4 book ai didi

javascript - 如何将嵌套数组的属性相加

转载 作者:行者123 更新时间:2023-11-28 19:06:04 25 4
gpt4 key购买 nike

我的逻辑没有什么问题。

我有一个 Accordion ,我拍了照片

enter image description here

标题 LIVE BETTING、MLB、MLB Props 和 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 的 Controller

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 的 Prop ,所以我需要获取每一个 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迭代数组。但我不知道如何完成这项任务

编辑

不要感到困惑,因为我上面提到了现场投注,我的意思是,我希望同样的情况发生在阵列中的每项运动中。

最佳答案

尝试:

// in view, replace
{{sport.leagues.length}}

// with
{{ total(sport) }}

//in controller, add scope method
$scope.total = function(sport) {
return _.sum(_.pluck(sport.leagues, 'offeringsAvailable'));
}

关于javascript - 如何将嵌套数组的属性相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31662562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com