1

我今天早上早些时候问了这个问题“这是我的 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"
},
];

我想计算每个“周”的“发件人:IN1”字段,例如每周:1145 我会得到:3,对于“发件人: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'}
]
});

由于我将 extjs 4 与 MVC 模型一起使用,因此我制作了一个控制器来控制按钮的事件,现在它看起来像这样:

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"))"

似乎我在引用我的商店时出错了。有任何想法吗?

4

2 回答 2

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}
于 2012-10-10T09:14:55.577 回答
0

如果您使用的是您提供的数组,您可以使用 mimikomi 的答案或类似的 ExtJS 版本:

这是一个小提琴

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']);

这是一个小提琴

有关通过 ajax 将 json 动态加载到商店的更多信息(使用代理)

于 2012-10-10T09:42:24.553 回答