1

我有一个如下所示的 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 出现 3 次。

到目前为止,我有一个循环可以挑选出所有类型 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);
    }
});
4

1 回答 1

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);
于 2012-12-26T18:31:02.560 回答