【发布时间】:2019-09-03 12:21:18
【问题描述】:
我有一个包含一些数据的数据对象,现在我想创建另一个对象mapdata2,它与数据具有相同的结构。但是我的代码不起作用,并且还显示了一些语法错误。
我在其中创建了 mapdata2 对象和空特征数组。
它显示一个错误:
TypeError: i.features 未定义
<script>
data = {"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties":
{
"title": "ABC", "startDate": 1100, "endDate": 1200, "latitude": 60.814, "longitude": 11.845, "content": "content."
},
"geometry":
{
"type": "Point","coordinates": [ 60.814, 11.845, 1]
}
},
{
"type": "Feature",
"properties":
{
"title": "XYZ", "startDate": 1100, "endDate": 1200, "latitude": 40.814, "longitude": 15.845, "content": "content."
},
"geometry":
{
"type": "Point","coordinates": [ 40.814, 15.845, 1]
}
},
]
}
mapdata2 = {
"type": "FeatureCollection",
"features" : []
};
for(i in data){
console.log(i);
mapdata2.features.push({
type:"Feature",
properties : { title: i.features.properties.title, startDate: i.features.properties.startDate, endDate: i.features.properties.endDate latitude: i.features.properties.latitude, longitude: i.features.properties.longitude, content: i.features.properties.content },
geometry : { type: "Point", coordinates: i.features.geometry.coordinates }
})
}
console.log(mapdata2);
</script>
【问题讨论】:
标签: javascript arrays json object