以下函数返回此输出。但我不明白为什么。有线索吗?
Output: {"A":{"antal":null},"B":{"antal":null},"C":{"antal":null},"D":{"antal":null},"E":{"antal":null},"G":{"antal":null}}
功能是,
function seriestat(){
var statserier = {};
$.each(globalSIEdata["#EXTRA"]["VERSERIER"], function(i, item) {
statserier[i] = {};
});
$.each(globalSIEdata["#VER"], function(i2, item2) {
var serie = i2.substring(0, i2.indexOf('-'));
statserier[serie]["antal"] += 1;
});
return statserier;
}
以下是来自globalSIEdata的示例:
{ "#VER": {
"A-1": {
"verdatum": "2017-01-03"
},
"A-2": {
"verdatum": "2017-01-03"
},
"B-1": {
"verdatum": "2017-01-03"
},
"B-2": {
"verdatum": "2017-01-03"
}
"A-3": {
"verdatum": "2017-01-03"
}
}
答案 0 :(得分:3)
你忘了初始化" antal"属性因此它将是未定义的,尝试类似:
statserier[serie]["antal"] = (statserier[serie]["antal"] || 0) + 1;
或者,您可以尝试按如下方式初始化您的statserier对象:
statserier[i] = { antal: 0 };