我有一个JSON商店:
{
"Week": 1145,
"From": "IN1"
},
{
"Week": 1145,
"From": "IN1"
},
{
"Week": 1145,
"From": "IN2"
},
{
"Week": 1146,
"From": "IN1"
},
{
"Week": 1146,
"From": "IN2"
}
我想计算每个“周”,“从”的数量,例如第1146周,我得到IN1 = 1和IN2 = 1而对于第1145周,我得到IN1 = 2和IN2 = 1。
我编写了一个循环遍历数据存储的函数,为每个参数计算IN1和IN2:
countBy: function(param){
var count = {};
this.each(function(item){
var type = item.get(param);
if (Ext.isDefined(count[type])){
count[type]++;
} else {
count[type] = 1;
}
});
return count;
}
但问题是,当我在param中给它“Week”时,每个WEEK不计入IN1和IN2,它返回“1145”:3和1146:2,但我想要的是:“1145 “:{IN1:2}和”1146“:{IN1:1}。
感谢您的帮助!
答案 0 :(得分:1)
您还需要传递From
作为参数。
尝试以下:
countBy: function(param, param2){
var count = {};
this.each(function(item){
var type = item.get(param);
var from = item.get(param2);
if (type in count){
if (from in count[type]) {
count[type][from]++;
} else {
count[type][from] = 1;
}
} else {
count[type] = {};
count[type][from] = 1;
}
});
return count;
}