0

I have an array of cities with this structure (given from the CMS):

  const cities = [
    {
      city: 'Genova',
      coordinates: '{\'type\':\'Point\',\'coordinates\':[8.9473343,44.4023918]}',
      countryIsoCode: 'it',
      description: 'test',
      isInitialCity: true,
    }, {

      city: 'Barcelona',
      coordinates: '{\'type\':\'Point\',\'coordinates\':[2.0951271,41.3397004]}',
      countryIsoCode: 'es',
      description: 'description',
      isInitialCity: false,
    }, {
      city: 'Sydney',
      coordinates: '{type\':\'Point\',\'coordinates\':[151.2158203,-33.8704156]}',
      countryIsoCode: 'au',
      description: 'Sydney description',
      isInitialCity: false,
    }];

I want to parse the coordinates position to get a more scalable object and get its properties nesting.

This is what I've tried:

cities.map(city=>JSON.parse(city.coordinates))

But when I print it seems to have no effect. However, if I manually print a position like console.log(JSON.parse(cities[0].coordinates)) it shows a formatted result like is shown in the following screenshot:

enter image description here

How can I make it automatically via loop?

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67

3 Answers3

2

This is what I've tried:

cities.map(city=>JSON.parse(city.coordinates))

map() created you a brand new, separate array with the coordinates only, which you have thrown away afterwards.

However, if I manually print a position like console.log(JSON.parse(cities[0].coordinates)) [...] How can I make it automatically via loop?

Well, put it in a loop:

for(let city of cities)
  city.coordinates = JSON.parse(city.coordinates);


However your example data is syntactically incorrect, there are ,}-s at the end of the objects (after the true/false), and the supposed JSON data is not JSON, like
{type':'Point','coordinates':[151.2158203,-33.8704156]}
     ^it has no pair, and it should be double quote anyway, all of them
{"type":"Point","coordinates":[151.2158203,-33.8704156]} <-- this is JSON
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Thanks, it works. Is there a way to use a one-line loop (`forEach`, etc)? – Ferran Buireu Apr 27 '20 at 08:32
  • 1
    `for..of` is a "more one-line loop" than `forEach()` in my opinion, but otherwise yes: `cities.forEach(city->city.coordinates=JSON.parse(city.coordinates));` – tevemadar Apr 27 '20 at 08:34
1

 const cities = [
    {
      city: 'Genova',
      coordinates: '{\'type\':\'Point\',\'coordinates\':[8.9473343,44.4023918]}',
      countryIsoCode: 'it',
      description: 'test',
      isInitialCity: true,
    }, {

      city: 'Barcelona',
      coordinates: '{\'type\':\'Point\',\'coordinates\':[2.0951271,41.3397004]}',
      countryIsoCode: 'es',
      description: 'description',
      isInitialCity: false,
    }, {
      city: 'Sydney',
      coordinates: '{type\':\'Point\',\'coordinates\':[151.2158203,-33.8704156]}',
      countryIsoCode: 'au',
      description: 'Sydney description',
      isInitialCity: false,
    }];
     
    //for(let city of cities)
 // city.coordinates = JSON.parse(city.coordinates);
    
    
    var x=cities.map(city=>JSON.parse(JSON.stringify(city.coordinates)))
   
   
   console.log("result :"+(JSON.stringify(x)))
  //   result :["{'type':'Point','coordinates':[8.9473343,44.4023918]}","{'type':'Point','coordinates':[2.0951271,41.3397004]}","{type':'Point','coordinates':[151.2158203,-33.8704156]}"]
Jadli
  • 858
  • 1
  • 9
  • 17
1

What I think might be happening is that you're doing the map right but not returning the result.

For example mynumbers.map(num => num++) won't actually effect mynumbers at all. You have to assign the result of the map to another variable...

const parsedCities = cities.map(city=>JSON.parse(city.coordinates))

Now your new parsedCities variable will look like you want it to and the original cities array will remain unchanged.

cd3k
  • 719
  • 5
  • 8