如何拆分字符串数组,将字符串数字解析为整数并将它们添加到 for 循环中
How to split an array of strings, parse the string numbers as integers and add them together in a for loop
我有一个字符串数组(分数形式的分数),例如[“40/60”、“30/40”、...]。分数表示用户对问题的回答,数组是给定类别的所有回答。
我想拆分字符串,将每个字符串解析为一个整数,然后在 for 循环中将每个左侧分数相加以获得用户的总类别分数,然后将每个右侧相加获得总可能最高分数的分数。
我已经尝试了下面的代码并返回了 NaN。
################
var scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
var i;
var myCategoryScore;
var maxCategoryScore;
################
for(i=0; i < scoreArray.length;i++){
var splitScore = scoreArray[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
myCategoryScore = myCategoryScore + myQuestionScore;
console.log(myCategoryScore);
maxQuestionScore = parseInt(splitScore[1]);
maxCategoryScore = maxCategoryScore + maxQuestionScore;
console.log(maxCategoryScore);
}
打印出来的结果是:
Array ["45", "60"]
NaN
NaN
Array ["60", "60"]
NaN
NaN
Array ["40", "40"]
NaN
NaN
etc
然而,当仅自行打印出已解析的整数时,很明显它们已被正确解析...
for(i=0; i<scoreArray.length;i++){
var splitScore = scoreArray[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
console.log(myQuestionScore);
maxQuestionScore = parseInt(splitScore[1]);
console.log(maxQuestionScore);
}
这导致..
Array ["45", "60"]
45
60
Array ["60", "60"]
60
60
Array ["40", "40"]
40
40
etc...
那为什么我不能把总数加在一起呢?我如何解决它?是由于范围?
谢谢!
myCategoryScore
从未初始化。
myCategoryScore = myCategoryScore + myQuestionScore;
将提供 undefined + 42 //NaN
maxCategoryScore
也一样
如何重现您的错误:
var UndefinedValue;
console.log(UndefinedValue);
console.log(UndefinedValue + 42);
初始化您的变量,以便您可以在数学运算中使用它们:
var scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
var i;
// initialize this one
var myCategoryScore = 0;
// and this one
var maxCategoryScore = 0;
for(i=0; i < scoreArray.length;i++){
var splitScore = scoreArray[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
myCategoryScore = myCategoryScore + myQuestionScore;
console.log(myCategoryScore);
maxQuestionScore = parseInt(splitScore[1]);
maxCategoryScore = maxCategoryScore + maxQuestionScore;
console.log(maxCategoryScore);
}
我认为 .reduce 函数在这里是个不错的选择:)
var scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
var scoreArrayParsed = scoreArray.reduce((acc, e) => {
let arr = e.split('/');
acc.min += parseInt(arr[0]);
acc.max += parseInt(arr[1]);
return acc;
}, {
min: 0,
max: 0
});
console.log(scoreArrayParsed);
如果你有 lodash 可用,你可以使用 _.zip
Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
_.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]
你可以试试这个:
const _ = require('lodash');
const scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
const sortedScores = _.zip(..._.map(scoreArray, s => s.split('/')));
// [ [ '45', '60', '40', '30', '15', '30', '30', '20' ],
// [ '60', '60', '40', '40', '20', '40', '60', '40' ] ]
第一个数组有第一个值,第二个井...有第二个值!在此之后,您可以继续使用 reduce
正如其他答案所建议的那样。 Lodash 还提供了一个 _.sum
函数,您可以使用它来节省一些开销:)
我有一个字符串数组(分数形式的分数),例如[“40/60”、“30/40”、...]。分数表示用户对问题的回答,数组是给定类别的所有回答。
我想拆分字符串,将每个字符串解析为一个整数,然后在 for 循环中将每个左侧分数相加以获得用户的总类别分数,然后将每个右侧相加获得总可能最高分数的分数。
我已经尝试了下面的代码并返回了 NaN。
################
var scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
var i;
var myCategoryScore;
var maxCategoryScore;
################
for(i=0; i < scoreArray.length;i++){
var splitScore = scoreArray[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
myCategoryScore = myCategoryScore + myQuestionScore;
console.log(myCategoryScore);
maxQuestionScore = parseInt(splitScore[1]);
maxCategoryScore = maxCategoryScore + maxQuestionScore;
console.log(maxCategoryScore);
}
打印出来的结果是:
Array ["45", "60"]
NaN
NaN
Array ["60", "60"]
NaN
NaN
Array ["40", "40"]
NaN
NaN
etc
然而,当仅自行打印出已解析的整数时,很明显它们已被正确解析...
for(i=0; i<scoreArray.length;i++){
var splitScore = scoreArray[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
console.log(myQuestionScore);
maxQuestionScore = parseInt(splitScore[1]);
console.log(maxQuestionScore);
}
这导致..
Array ["45", "60"]
45
60
Array ["60", "60"]
60
60
Array ["40", "40"]
40
40
etc...
那为什么我不能把总数加在一起呢?我如何解决它?是由于范围?
谢谢!
myCategoryScore
从未初始化。
myCategoryScore = myCategoryScore + myQuestionScore;
将提供 undefined + 42 //NaN
maxCategoryScore
如何重现您的错误:
var UndefinedValue;
console.log(UndefinedValue);
console.log(UndefinedValue + 42);
初始化您的变量,以便您可以在数学运算中使用它们:
var scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
var i;
// initialize this one
var myCategoryScore = 0;
// and this one
var maxCategoryScore = 0;
for(i=0; i < scoreArray.length;i++){
var splitScore = scoreArray[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
myCategoryScore = myCategoryScore + myQuestionScore;
console.log(myCategoryScore);
maxQuestionScore = parseInt(splitScore[1]);
maxCategoryScore = maxCategoryScore + maxQuestionScore;
console.log(maxCategoryScore);
}
我认为 .reduce 函数在这里是个不错的选择:)
var scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
var scoreArrayParsed = scoreArray.reduce((acc, e) => {
let arr = e.split('/');
acc.min += parseInt(arr[0]);
acc.max += parseInt(arr[1]);
return acc;
}, {
min: 0,
max: 0
});
console.log(scoreArrayParsed);
如果你有 lodash 可用,你可以使用 _.zip
Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
_.zip(['a', 'b'], [1, 2], [true, false]); // => [['a', 1, true], ['b', 2, false]]
你可以试试这个:
const _ = require('lodash');
const scoreArray = ["45/60", "60/60", "40/40","30/40", "15/20", "30/40", "30/60", "20/40"];
const sortedScores = _.zip(..._.map(scoreArray, s => s.split('/')));
// [ [ '45', '60', '40', '30', '15', '30', '30', '20' ],
// [ '60', '60', '40', '40', '20', '40', '60', '40' ] ]
第一个数组有第一个值,第二个井...有第二个值!在此之后,您可以继续使用 reduce
正如其他答案所建议的那样。 Lodash 还提供了一个 _.sum
函数,您可以使用它来节省一些开销:)