我有以下 JavaScript 语法:
console.log(printClassified(classifyByDayOfWeek(myData)));
mySundaysData = new Array(printClassified(classifyByDayOfWeek(myData)));
console.log(mySundaysData);
var mySundaysTotal = 0;
for(var i=0, len=mySundaysData.length; i<len; i++){
mySundaysTotal += parseFloat(mySundaysData[i][1]);
}
console.log(mySundaysTotal);
谷歌浏览器控制台:
printClassified(classifyByDayOfWeek(myData)) = [2013-04-21, 1][2013-04-28, 0][2013-05-05, 2][2013-05-12, 0]
mySundaysData = ["[2013-04-21, 1][2013-04-28, 0][2013-05-05, 2][2013-05-12, 0]"]
mySundaysTotal = 2
我想计算数组中的值,对于上面的例子:1 + 0 + 2 + 0 它应该给我 3 而它给我 2 就像它可以在console.log(mySundaysTotal)
. 即使我有其他数组 mySundaysTotal 始终为 2。为什么?我做错了什么?
编辑:其余代码:
function sorter(a, b) {
var d1 = new Date(a[0].replace("-", "/")).getDay();
var d2 = new Date(b[0].replace("-", "/")).getDay();
return d1 - d2;
}
function classifyByDayOfWeek(customArr) {
var byDayOfWeek = [[], [], [], [], [], [], []];
for (var i = 0; i < customArr.length; i++) {
var day = new Date(customArr[i][0]).getDay();
byDayOfWeek[day].push(customArr[i]);
};
return byDayOfWeek;
}
function printRaw(arr) {
str = "";
for (var i = 0; i < arr.length; i++) {
str += "[" + arr[i][0] + ", " + arr[i][1] + "]";
}
return str;
}
function printClassified(arr) {
str = "";
str += printRaw(arr[0]);
return str;
}
JSFiddle - http://jsfiddle.net/DanielaVaduva/NUHFu/