3

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>

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. @Omri Attiya. Now its working. I have a question. In my data there is also an array 'coordinates' in geometry. if i want to add data dynamically in this array so do i need to create one more array on top. ?
@MuhammadHassan If you want to add data to an array, you just need to use push. For example: mapdata2.features[0].geometry.coordinates.push(777);
3

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

Comments

2

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

Comments

2

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)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.