我有一个包含一些数据的数据对象,现在我想创建另一个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>
如果您只希望将json列表复制到mapdata2,则根本不需要映射或循环。
mapdata2.features = data.features
console.log(mapdata2);
这将完成您希望完成的任务,而无需循环。
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" : []
};
mapdata2.features = data.features
console.log(mapdata2);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句