我有一个看起来像这样的JSON:
({
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"wkb_geometry": null,
"code": "type 1",
"code2": "type 3",
"code3": "type 5",
"updated_at": "2012-10-29T12:38:00.037Z"
},
},
{
"type": "Feature",
"properties": {
"wkb_geometry": null,
"code": "type 1",
"code2": "type 5",
"code3": "type 7",
"updated_at": "2012-10-29T12:38:00.037Z"
},
},
{
"type": "Feature",
"properties": {
"wkb_geometry": null,
"code": "type 2",
"code2": "type 3",
"code3": "type 5",
"activity_type": "children's play area",
"updated_at": "2012-10-29T12:38:00.037Z"
},
}...
基本上我想计算这个Json中的所有代码,即有5次出现类型。
到目前为止,我有一个循环可以找出所有类型1代码,但我不知道如何让循环计算项目:
$.each(geojson.features, function(i, v) {
if (v.properties.code.search(new RegExp(/type 1/i)) != -1) {
console.log(v.properties.activity_type_code);
}
});
答案 0 :(得分:3)
您只需要在if语句中增加一个计数器:
var count = 0;
$.each(geojson.features, function (i, v) {
if (v.properties.code.search(new RegExp(/type 1/i)) != -1) {
count++;
}
});
console.log(count);