0

I'm getting this dynamic response from the server . and here i want to push my mapD data into mapData (i.e Dynamic Data);

[
      {
        Name: "Aura",
        OrderState: "2",
        latitude: "28.197619999999997",
        longitude: "65.24778333333334"
      }
    ]

I store the above dynamic response as this.mapData = res.Data .

I have a static data too e.g.

public mapD:any=[{
    Name: "mad",
    OrderState: "2",
    latitude: "39.6868",
    longitude: "83.2185"
    }]

I want this above static object to contain the dynamic object so that I will get 2 objects in total - I'm using push method but unable to get the data using

this.mapD.push(this.mapData)

and im using mapData for saving the response and looping it in the template

4
  • I will get 2 objects in total - you mean two objects in the mapD array? Commented Mar 28, 2018 at 14:09
  • Let newArr = [...firstArray, ...secondArray]; Commented Mar 28, 2018 at 14:10
  • @mplungjan no in mapData Commented Mar 28, 2018 at 14:11
  • You received an Array. If the array always has only a uniqe element you must do this.mapD.pus(this.mapData[0]) Commented Mar 28, 2018 at 14:21

3 Answers 3

1

You cannot push an array into another. Use spread instead:

this.mapD.push(...this.mapData);

Or array.concat if you cannot use ES6:

this.mapD = this.mapD.concat(this.mapData);
Sign up to request clarification or add additional context in comments.

3 Comments

by using spread operator im getting this below error --Cannot read property 'apply' of undefined-- im using mapData for saving the response and looping it in the templatethis.mapData
Initialize your map data like this.mapData=[];
you can use safe call like this this.mapD.push(...(this.mapData || []));
0

I'm not sure of what you mean, but if you want to merge the returned array mapData into the existing one mapD, you could try the following:

Array.prototype.push.apply(this.mapD, this.mapData);

It will store the result in this.mapD.

Hope this helps,

Comments

0

You could declare mapD as an array of mapData so you could later access you object properties. A simple example:

mapData: Array<any>
mapD: Array<any>

getResponse() {
   this.yourApiService.subscribe(
      (response: Array<any>) => {
          this.mapDdata = response;
          // Loop through the response array here or push first item
          this.mapD.push(reponse[0]);
      }
   );
}

Now you have mapD which is of type array and contains your mapped mapData objects. This allows you to access the properties of the objects stored on that array.

2 Comments

im storing my respose in mapData
I have updated the above to add your response under mapData. If you wish to append the response into your static array you should push it right after the response.

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.