admin管理员组文章数量:1417426
I have a data object which has some data now i want to create another object mapdata2
which has same structure as data. but my code did not work and also shows some syntax error.
I have created mapdata2
object and empty features array inside it.
It shows an error:
TypeError: i.features is undefined
<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>
I have a data object which has some data now i want to create another object mapdata2
which has same structure as data. but my code did not work and also shows some syntax error.
I have created mapdata2
object and empty features array inside it.
It shows an error:
TypeError: i.features is undefined
<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>
Share
Improve this question
edited Sep 3, 2019 at 13:44
Naveen
1,4612 gold badges16 silver badges41 bronze badges
asked Sep 3, 2019 at 12:21
Muhammad HassanMuhammad Hassan
10910 bronze badges
5 Answers
Reset to default 4That happens because you are trying to access features
for each i
in data
, but the first i
is "type"
and it doesn't have features
in it.
So I modified your code so that you iterate only over the "features", and for each feature you do what you did, and now it's working.
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": []
};
data.features.forEach((feature) => {
mapdata2.features.push({
type: "Feature",
properties: {
title: feature.properties.title,
startDate: feature.properties.startDate,
endDate: feature.properties.endDate,
id: feature.properties.id,
latitude: feature.properties.latitude,
longitude: feature.properties.longitude,
content: feature.properties.content
},
geometry: {
type: "Point",
coordinates: feature.geometry.coordinates
}
})
});
console.log(mapdata2);
You dont need to map or loop at all if you simply wish to copy the json list to mapdata2.
mapdata2.features = data.features
console.log(mapdata2);
This will do what you wish to acplish without having to loop at all.
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);
It seems you want to copy the object. You can simply do as below.
var mapdata2 = JSON.parse(JSON.stringify(data));
By the way, you got error "TypeError: i.features is undefined" because i is a string representing a key of the object. It will take the values "type" and "features".
To loop over the items in the array data.features, you should do:
for (var i=0;i<data.features.length;i++) {
var item = data.features[i];
}
There are some errors here:
in
keyword in a for bucle iterates over keys, not values, which is what I understand you want to do to copy your data.- you are trying to access property
properties
of the arrayfeatures
without accessing an index of that array first.
Changing the last part of your code to:
for(const feature of data.features){
mapdata2.features.push({
type:"Feature",
properties : { title: feature.properties.title, startDate: feature.properties.startDate, endDate: feature.properties.endDate, latitude: feature.properties.latitude, longitude: feature.properties.longitude, content: feature.properties.content },
geometry : { type: "Point", coordinates: feature.geometry.coordinates }
})
}
console.log(mapdata2);
should do the job :)
However, if you are just intending to copy the data I would remend:
mapdata2 = {...data}
console.log(mapdata2)
a 'quick fix' maintaining your style:
for(i of data.features) {
console.log(i)
mapdata2.features.push({
type:"Feature",
properties : {
title: i.properties.title,
startDate: i.properties.startDate,
endDate: i.properties.endDate,
id: i.properties.id,
latitude: i.properties.latitude,
longitude: i.properties.longitude,
content: i.properties.content },
geometry : {
type: "Point",
coordinates: i.geometry.coordinates
}
})
}
本文标签: javascriptHow to add data in json object from another objectStack Overflow
版权声明:本文标题:javascript - How to add data in json object from another object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745264697a2650530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论