Muhammad Hassan
Muhammad Hassan

Reputation: 109

How to add data in json object from another object?

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>

Upvotes: 3

Views: 457

Answers (5)

Marcos Luis Delgado
Marcos Luis Delgado

Reputation: 1429

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 array features 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 recommend:

mapdata2 = {...data}
console.log(mapdata2)

Upvotes: 2

Nabeel Mehmood
Nabeel Mehmood

Reputation: 398

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 accomplish 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);

Upvotes: 3

Gabriele Magno
Gabriele Magno

Reputation: 196

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 
        }
    })
}

Upvotes: 1

Omri Attiya
Omri Attiya

Reputation: 4037

That happens because you are trying to access features for each i in data, but the first iis "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);

Upvotes: 4

Guillaume Adam
Guillaume Adam

Reputation: 301

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];
}

Upvotes: 2

Related Questions