Bas Bas - 1 month ago 20
TypeScript Question

Angular 2 make array/object by recieved json http get

I recieving a json to an

items:any = [];

items[0]['name']
items[0]['adres']
items[0]['lat']
items[0]['lng']


In this i have 4 keys and need only 2, lat and lng.

How can i make an markers array like this dynamic from the items in my app.component.ts?

markers = [
{
lat: 51.673858,
lng: 7.815982
},
{
lat: 51.373858,
lng: 7.215982
},
{
lat: 51.723858,
lng: 7.895982
}
]

Answer
var n = 0;
var markers = items.map(item => {
    lat: item['lat'],
    lng: item['lng'],
    label: String.fromCharCode(65 + n++)
});

Btw, this is not related to Angular2 but TypeScript instead.