gpt4 book ai didi

javascript - 如何有效地从 geojson 中提取值?

转载 作者:行者123 更新时间:2023-11-30 13:14:12 25 4
gpt4 key购买 nike

我有一个具有以下结构的 geojson 文件:

var areas = {
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "id": 0, "properties": { "name": "a", "count": "854" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.271687552328165, 29.359549285088008 ], [ 13.272222904657671, 29.357697459147403 ], [ 13.272586765837973, 29.35566049412985 ], [ 13.273062097784726, 29.354438105832578 ], [ 13.272652418199639, 29.360795108476978 ], [ 13.271320041078822, 29.360700647951568 ], [ 13.271687552328165, 29.359549285088008 ] ] ] } }
,
{ "type": "Feature", "id": 1, "properties": { "name": "b", "count": "254"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.277038163109875, 29.358442424220023 ], [ 13.276782949188294, 29.358122923383512 ], [ 13.275290999273452, 29.358508600578681 ], [ 13.274634727185679, 29.358485484466968 ], [ 13.282581930993208, 29.358635779719847 ], [ 13.278334024184868, 29.359814295404375 ], [ 13.277038163109875, 29.358442424220023 ] ] ] } }
,
{ "type": "Feature", "id": 2, "properties": {"name": "c", "count": "385"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.279484097462499, 29.349831113628788 ], [ 13.27792879702741, 29.349728966688758 ], [ 13.276089401418951, 29.349901260351807 ], [ 13.275677565886326, 29.349951087700632 ], [ 13.279484097462499, 29.349831113628788 ] ] ] } }
,
{ "type": "Feature", "id": 3, "properties": { "name": "d", "count": "243"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.290520299215724, 29.352306689134622 ], [ 13.289722088408338, 29.351802774685929 ], [ 13.289065241087885, 29.352101541350635 ], [ 13.28785814146197, 29.351114667998772 ], [ 13.290520299215724, 29.352306689134622 ] ] ] } }

]
}

通常,要将此数据转换为数组,我会创建一个循环来遍历将此值存储在数组中的适当变量。但是,我不知道这是否是最好的方法。有没有一种方法可以将 id 的所有值提取到一个数组中,这样我最终会得到:

[0,1,2,3]

我也很乐意使用任何外部库来执行此操作 - 我主要想知道是否有比我当前方法更有效的方法。

最佳答案

不是我的代码,但这里有一篇关于非常有效地从 json 获取值的帖子。

引用帖子 use jQuery's find() on JSON object

User Box9 的回答可能是最好的方法

function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else if (i == key && obj[key] == val) {
objects.push(obj);
}
}
return objects;
}

也对这个代码块的答案投了赞成票,因为这是我发现提取值的最佳方式

关于javascript - 如何有效地从 geojson 中提取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12733201/

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