gpt4 book ai didi

jquery计算json中的项目数

转载 作者:行者123 更新时间:2023-12-01 08:10:24 24 4
gpt4 key购买 nike

我有一个如下所示的 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);
}
});

最佳答案

您只需在 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);

关于jquery计算json中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com