0

This is a question of an extension of a previously asked question. I have data in the ngOnInit see below and these fields are nor the same as the markers object.

Data from http get in my data.service.ts appending to

items:any = [];

ngOnInit() {
    this.dataService.fetchData(this.slug)
        .subscribe(
            (data) => {
                this.checkData(data);//in this function its going to be appending to this.items
        }
    );
}


checkData(data) {
    if (data.length === 0) {
        return this.router.navigate(['']);
    }
    return this.items = data;
}

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

]

So how can i overwrite the existing markers object with my this.items object (in this object is lat and lng available).

removing qoutes

Awnser was correct from @Eric N and made the marker.latitude and marker.longitude parseFloat().

2
  • Can you assign items to the array of initial marker data at first? Then your checkData func can reassign items and effectively overwrite it. Commented Oct 25, 2016 at 11:55
  • @EricN but how can i overwrite these? The item array contains not identical fields. Its name, latitude and longitude. Commented Oct 25, 2016 at 12:03

1 Answer 1

1

Oh is your app mapping to markers.lat and markers.lng but you are receiving data.latitude and data.longitude? It'd be a lot cleaner to change your markers and app to use the same keys as your data source. But if your stuck using two types of data objects, you can map your received data to a new object with the right keys.

let newItems:any[] = [];

for (var element in data) {  // map over the data type
  let marker = data[element];
  newItems.push(
    { 
        lat: marker.latitude, // create new objects matching the keys used in the app
        lng: marker.longitude
    }
}

this.items = newItems;  // and assign
Sign up to request clarification or add additional context in comments.

4 Comments

Or just this.items = data.map(m => {lat: m.latitude, lng: m.longitude});
Too early in the morning for intelligent mapping functions =)
its returning an object and the lat en lng are string, how can i make them as number?
parseFloat(string)

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.