gpt4 book ai didi

javascript - 通过点表示法访问 JavaScript 对象

转载 作者:行者123 更新时间:2023-11-28 18:10:16 24 4
gpt4 key购买 nike

我有一个具有该结构的 JavaScript 对象

myJsObject= {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [84.1, 99.1]
},
"properties": {
"value": "20",
"s": "abc"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [18.15, 9.73]
},
"properties": {
"value": "0",
"s": "def"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1.15, 4.78]
},
"properties": {
"value": "10",
"s": "fdg"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [17.15, 5.13]
},
"properties": {
"value": "5",
"s": "dfs"
}
}]
}

我尝试以这种方式访问​​它:

myJsObject.features.forEach(function(d) {
d.LatLng = new L.LatLng(d.geometry.coordinates[1],
d.geometry.coordinates[0]);
});

这让我返回未定义。我可以像这样访问

myJsObject.features;

像这样

myJsObject.features[0];

但不是这样的

myJsObject.features.geometry;

这是为什么呢?我需要后者用于 fr forEach 循环...

最佳答案

Why is that?

myJson.features 的值是一个数组。

它不是一个具有名为 geometry 属性的对象。

数组包含具有名为geometry属性的对象。

关于javascript - 通过点表示法访问 JavaScript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41767989/

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