gpt4 book ai didi

javascript - 添加属性backbone.js

转载 作者:行者123 更新时间:2023-12-02 17:51:21 25 4
gpt4 key购买 nike

homeTeam = this.collection.where({teamName: 'Bulls'});

var totalPoints = [];
_.each(homeTeam, function(team) {
var total = team.get('points');
totalPoints.push(total);
});

var sum = _.reduce(totalPoints, function(memo, num){ return memo + num; }, 0);
console.log(sum);

在上面,我试图通过迭代该属性,然后将这些值插入数组来获取主队拥有的总积分。最后我使用了 underscore.js 的 _.reduce 方法,但我在控制台中没有得到正确的数字。

实际的点数是 10,12,18,3,0,0,当我 console.log(sum) 时,我得到 0101218300,​​所以它将所有这些单独的数字变成一个巨大的数字而不是通过添加总和但将它们组合起来。

很明显我遗漏了一些东西,希望有一种比我现在的方式更好的方法来添加属性。

最佳答案

发生这种情况是因为总点数存储为字符串数组。尝试一下

.each(homeTeam, function(team) {
//convert the string to int
var total = parseInt(team.get('points'),10);
totalPoints.push(total);
});

关于javascript - 添加属性backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339198/

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