计算JSON某个数据的数量
[
{"id": 123, "type": "1"},
{"id": 234, "type": "1"},
{"id": 345, "type": "2"},
{"id": 4646, "type": "2"},
{"id": 878, "type": "2"},
{"id": 78987, "type": "2"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
]
var json = [
{"id": 123, "type": "1"},
{"id": 234, "type": "1"},
{"id": 345, "type": "2"},
{"id": 4646, "type": "2"},
{"id": 878, "type": "2"},
{"id": 78987, "type": "2"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
{"id": 4646, "type": "3"},
];
var count = 0;
for(var i=0;i<json.length;i++){
count += json[i].type=="2"?1:0;
}
alert(count);
Array.prototype.FindAll = function(fn){
if(typeof(fn) === 'function'){
ret = [];
for(var i = 0, l = this.length; i<l; i++){
var o = this[i];
if(fn(o)) ret[ret.length] = o;
}
return ret;
}
return null;
}
var arr = 上面定义的那些数据;
alert(arr.FindAll(function(o){return o.type == "1";}).length);