Bas
Bas

Reputation: 2400

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

Upvotes: 1

Views: 374

Answers (1)

martin
martin

Reputation: 96891

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.

Upvotes: 3

Related Questions