今天早上我问过这个问题 “这是我的JSON文件:
[
{
"Week": "1145",
"Sev_Logged": "3_major",
"From": "IN1"
},
{
"Week": "1145",
"Sev_Logged": "4_minor",
"From": "IN1"
},
{
"Week": "1145",
"Sev_Logged": "4_minor",
"From": "IN1"
},
{
"Week": "1145",
"Sev_Logged": "4_minor",
"From": "IN1"
},
{
"Week": "1145",
"Sev_Logged": "4_minor",
"From": "IN1"
},
{
"Week": "1145",
"Sev_Logged": "4_minor",
"From": "IN2"
},
{
"Week": "1145",
"Sev_Logged": "3_major",
"From": "IN2"
},
];
我想计算每个“周”的“from:IN1”字段,每个例子一周:1145我得到:3,而“From:IN2”我得到2
谢谢“
感谢VDP的回答,所以我这样做了: 我的商店现在像:
Ext.define('Metrics.store.GraphData', {
extend : 'Ext.data.Store',
model : 'Metrics.model.GraphData',
autoload : true,
proxy : {
type : 'ajax',
url : 'data/metrics_data.json',
reader : {
type : 'json',
root : 'data',
successProperty : 'success'
}
},
//data : GraphData, //povide inline data or load using proxy
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;
}
});
我的模特:
Ext.define('Metrics.model.GraphData', {
extend: 'Ext.data.Model',
//fields: ['week', 'sev']
fields : [
{name: 'Week', type: 'int'},
{name: 'Sev_Logged', type: 'string'},
{name: 'From', type: 'string'}
]
});
由于我正在使用带有MVC模型的extjs 4,我已经制作了一个控制按钮事件的控制器,现在它看起来像这样:
launchGraph : function(button){
console.log('clicked the apply button');
var chartStore = this.getGraphDataStore();
chartStore.load({
callback: this.onGraphDataLoad,
scope: this,
console.log(chartStore.countBy("From"))
});
但是当我点击“应用”按钮时,我在控制器中收到此错误:
"Uncaught SyntaxError: Unexpected token . "
它指向了这一行:
"console.log(chartStore.countBy("From"))"
好像我在引用我的商店时出错了。有什么想法吗?
答案 0 :(得分:1)
如果var jsonData
是解析上面显示的json得到的数组,那么这将为您提供一个对象,其中每个对象的数量取决于您使用的参数。
function CountBy(parameter) {
var count = {};
for (var i = 0; i < jsonData.length; i++) {
var type = jsonData[i][parameter];
if (count[type] === undefined) {
count[type] = 1;
} else {
count[type]++;
}
}
return count;
}
结果:
CountBy("From") => {"IN1" : 5, "IN2" : 2}
CountBy("Week") => {"1145" : 7}
CountBy("Sev_Logged") => {"3_major": 2, "4_minor": 5}
答案 1 :(得分:0)
如果您使用的是您提供的阵列,您可以使用mimikomi的答案或类似的ExtJS版本:
这是一个fiddle
function countBy(data, param) {
var count = {};
Ext.each(data, function(item){
var type = item[param];
if (Ext.isDefined(count[type])) {
count[type]++;
} else {
count[type] = 1;
}
});
return count;
}
如果从外部位置加载json,最好将其加载到商店中。例如,您可以定义模型,商店并向商店添加“特殊”功能。
Ext.define('Week', {
extend: 'Ext.data.Model',
fields: [
{name: 'Week', type: 'int'},
{name: 'Sev_Logged', type: 'string'},
{name: 'From', type: 'string'}
]
});
var store = Ext.create('Ext.data.Store', {
model: 'Week',
data : data, //povide inline data or load using proxy
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;
}
});
var countObject = store.countBy("From");
alert(Ext.encode(countObject));
//chrome developper tools, safari, opera can show us the contents of objects in a log too. IE, FF however only tell us it is an object. (pretty useless)
console.log(countObject);
//you can chose you're favorite notation to retreve the value (but I hope you knew this, it's pretty basic javascript)
console.log(countObject.IN1);
console.log(countObject['IN2']);
这是fiddle。
More info通过ajax(使用代理)将json动态加载到商店中