我的控制台向我显示了所有分数的总和,但是我希望它显示32和35而不是65。最终结果是我想将每个总和压入一个数组。
var peopleArray = [
{
name: "Hector Valdes",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "1",
"2", "5",
"4", "1"
]
}, {
name: "Tyler Williams",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "2",
"2", "5",
"4", "1"
]
}
]
let total = 0;
for (i = 0; i < peopleArray.length; i++){
for(j=0; j < peopleArray[i].scores.length; j++){
total += Number(peopleArray[i].scores[j]);
console.log(total);
};
};
答案 0 :(得分:1)
var peopleArray = [
{
name: "Hector Valdes",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "1",
"2", "5",
"4", "1"
]
}, {
name: "Tyler Williams",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "2",
"2", "5",
"4", "1"
]
}
]
let total = peopleArray.map(i => {
return i.scores.reduce((a, b) => parseInt(a) + parseInt(b), 0)
})
console.log(total)
答案 1 :(得分:1)
您可以使用map
遍历每个peopleArray
元素。使用reduce
对分数求和。
通过在字符串前添加+
,将字符串转换为数字的一个选项。
var peopleArray = [{"name":"Hector Valdes","photo":"","scores":["5","1","4","4","5","1","2","5","4","1"]},{"name":"Tyler Williams","photo":"","scores":["5","1","4","4","5","2","2","5","4","1"]}];
var total = peopleArray.map(o => o.scores.reduce((c, v) => +c + +v));
console.log(total);
答案 2 :(得分:0)
尝试一下
static createCar(req, res) {
const { errors, isValid } = validateNewCar(req.body);
if (!isValid) {
return res.status(400).json({ errors });
}
const vehicle = {
id: vehicles.length + 1,
userId: 3,
state: req.body.state,
status: 'available',
price: req.body.price,
manufacturer: req.body.manufacturer,
model: req.body.model,
bodyType: req.body.bodyType
};
vehicles.push(vehicle);
return res.status(201).json({
status: 201,
message: 'Vehicle created successfully',
data: vehicle,
});
}
答案 3 :(得分:0)
尝试一下
您需要在第一个循环中移动
+----+---------+--------------+----------------+-------+---------------------------+ | id | team_id | scoreable_id | scoreable_type | state | created_at | +----+---------+--------------+----------------+-------+---------------------------+ | 1 | 3979 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 2 | 3717 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 3 | 3626 | 8 | SpecialPlace | draft | 2015-11-30 10:09:06 +0000 | | 4 | 3202 | 8 | SpecialPlace | draft | 2015-11-30 10:09:06 +0000 | | 5 | 703 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 6 | 278 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 7 | 3166 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 8 | 3147 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 9 | 3146 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | | 10 | 3145 | 2 | SpecialEvent | draft | 2015-11-30 10:09:06 +0000 | +----+---------+--------------+----------------+-------+---------------------------+
,然后只需在数组中添加一个新的总对象即可。
declare @stmt1 varchar(max)
set @stmt1='insert into dbo.abcd SELECT [company],[address],
[address2],[zip],[zip4],[city],[state],
[telephone_number],LOWER([email]),[name],[fname],[mname],[lname],
[title_full] FROM DBO.filter WHERE
[id] in (''7'',''8'',''11'',''15'') and contains
(code,'+''''+'"0111" OR "4142"......................OR
"5999"'+''''+')'
exec (@stmt1) AT [server]
答案 4 :(得分:0)
尝试一下,希望对您有所帮助。
let scoresArr = peopleArray.map(data => {
return data.scores.reduce((a,b) => parseFloat(a) + parseFloat(b), 0)
})
console.log(scoresArr)