2

I'm trying to map a nested array using .map(), so I could display and pinpoint all London underground locations in a map.

this.undergroundGeoJson = [
        {
            'type': 'FeatureCollection',

            'crs': { 'type': 'name', 'properties': { 'name': 
            'urn:ogc:def:crs:OGC:1.3:CRS84' } },

            'features': [
                {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [-0.278126, 51.5025]
                    },
                    'properties': {
                        'name': 'Acton Town'
                    }
                },
                {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [-0.263033174, 51.50883531]
                    },
                    'properties': {
                        'name': 'Acton Central'
                    }
                },
                {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [-0.262879534, 51.50856013]
                    },
                    'properties': {
                        'name': 'Acton Central'
                    }
                }
           }
       ]

I need the coordinates array elements down in the geometry obj.

this is my code so far...

@computed
    get undergroundLatLongs() {
    return this.undergroundGeoJson.map((u) =>
    [u.features.geometry.coordinates[0], 
    u.features.geometry.coordinates[1]]);
}

and this is the error log...

Uncaught TypeError: Cannot read property 'coordinates' of undefined

any help welcomed.

3
  • 1
    Provide sample output that you want Commented Feb 1, 2019 at 9:13
  • 5
    feautres is an array, not an object, so you can't use object notation for it. It should be something like features[0].geometry Commented Feb 1, 2019 at 9:13
  • 1
    As pointed out by Jayce444, you need a index into the features array. Commented Feb 1, 2019 at 9:14

4 Answers 4

6

features is an array and you need to access it using index

 u.features[i].geometry.coordinates[0]
           ^^^

const undergroundGeoJson =[{'type':'FeatureCollection','crs':{'type':'name','properties':{'name':'urn:ogc:def:crs:OGC:1.3:CRS84'}},'features':[{'type':'Feature','geometry':{'type':'Point','coordinates':[-0.278126,51.5025]},'properties':{'name':'ActonTown'}},{'type':'Feature','geometry':{'type':'Point','coordinates':[-0.263033174,51.50883531]},'properties':{'name':'ActonCentral'}},{'type':'Feature','geometry':{'type':'Point','coordinates':[-0.262879534,51.50856013]},'properties':{'name':'ActonCentral'}}],}];

const ret = undergroundGeoJson.map((u,i) => [
  u.features[i].geometry.coordinates[0],
  u.features[i].geometry.coordinates[1],
]);

console.log(ret);

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

1 Comment

Thanks for that, it did work. However, I'm only getting the first set of coordinates. Any idea why?
2

You are trying to access to a property geometry of an array features which is wrong So you have to map it like this

u.features.map(f => f.geometry.coordinates[0])

Your final code should be like this

this.undergroundGeoJson = [{
  'type': 'FeatureCollection',

  'crs': {
    'type': 'name',
    'properties': {
      'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'
    }
  },

  'features': [{
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [-0.278126, 51.5025]
      },
      'properties': {
        'name': 'Acton Town'
      }
    },
    {
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [-0.263033174, 51.50883531]
      },
      'properties': {
        'name': 'Acton Central'
      }
    },
    {
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [-0.262879534, 51.50856013]
      },
      'properties': {
        'name': 'Acton Central'
      }
    }
  ]
}]
function undergroundLatLongs() {
 return this.undergroundGeoJson.map((u) =>
    [u.features.map(f => f.geometry.coordinates[0]), 
    u.features.map(f => f.geometry.coordinates[1])]);
}
console.log(undergroundLatLongs());

Comments

0

You were trying to use .map() on the undergroundGeoJson object. .map() can only be used on arrays. I believe you are trying to iterate through the array of objects in this.undergroundGeoJson.features? You have to do this instead:

this.undergroundGeoJson.features.map(u => {
   console.log(u) // this will print each object within `features`
   return u; // don't forget to return something
})

Comments

0

Since map works in looping through arrays, you can start the mapping from this.undergroundGeoJson[0].features

this.undergroundGeoJson = [{
  'type': 'FeatureCollection',

  'crs': {
    'type': 'name',
    'properties': {
      'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'
    }
  },

  'features': [{
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [-0.278126, 51.5025]
      },
      'properties': {
        'name': 'Acton Town'
      }
    },
    {
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [-0.263033174, 51.50883531]
      },
      'properties': {
        'name': 'Acton Central'
      }
    },
    {
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [-0.262879534, 51.50856013]
      },
      'properties': {
        'name': 'Acton Central'
      }
    }
  ]
}]
function undergroundLatLongs() {
return this.undergroundGeoJson[0].features.map((u) =>
[u.geometry.coordinates[0], 
u.geometry.coordinates[1]]);
}
var x= undergroundLatLongs();
console.log(x);

4 Comments

Will this code may answer the question, an explanation of what you changed would highly help the OP
You are map function will return undefined, you just want to consol.log, why not try forEach?
@Treycos Changes made as suggested!
@Ashish Changes made as suggested!

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.