0

I have the following response from my http server:

sites

[
  {
    "siteName": "Site de Marseille",
    "siteAdress1": "rue de blabla",
    "siteAddress2": "string",
    "siteCodPost": "13010",
    "siteTown": "Marseille",
    "points": [
      { "lat": 44.841225, "lng": -0.580036 },
      { "lat": 44.842236, "lng": -0.64696 },
      { "lat": 44.805615, "lng": -0.63084 }
    ],
    "id": "5d0ce7c4a06b07213a87a753",
    "companyId": "5cd430745304a21b9464a219",
    "customerId": "5cd430c65304a21b9464a21a"
  },
  {
    "siteName": "Site de Bordeaux",
    "siteAdress1": "rue de la  Garonne",
    "siteAddress2": "string",
    "siteCodPost": "31000",
    "siteTown": "Bordeau    x",
    "points": [
      { "lat": 44.819868, "lng": -0.582811 },
      { "lat": 44.853709, "lng": -0.483573 },
      { "lat": 44.80696, "lng": -0.53299 },
      { "lat": 44.80696, "lng": -0.629078 }
    ],
    "id": "5d0cf65fa06b07213a87a754",
    "companyId": "5cd430745304a21b9464a219",
    "customerId": "5cd430c65304a21b9464a21a"
  }
] 

These are 2 sites and for each site a property called points that is an array. My wish is to extract each points arrays independently and push them to another array. This new array will be an array of arrays.

For this, I have this code:

this.customerApi.getSites(this.currentUser.id)
.subscribe(response => {
  this.sites = response;
  const ok = this.sites.map(s => s.points);
  this.rectangles.push(ok);
});

I am getting an Observable() from my API (see above), and I use the .map transformation. My problem is that it creates an array and my code pushes that array composed by arrays of points.

I want to push each array of points instead of an array of array (created by .map) of points.

Is there please any simple way to resolve this?

Best Regards.

nash11
  • 8,220
  • 3
  • 19
  • 55
Philippe Corrèges
  • 663
  • 1
  • 11
  • 31
  • 1
    https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating – Dimanoid Jun 27 '19 at 09:06
  • 1
    You either execute the push via a `forEach` on each subarray or you apply the spread syntax depending on what `push` can work with. – claasic Jun 27 '19 at 09:10

2 Answers2

1
ok.forEach((points) => this.rectangles.push(points))
Shanon Jackson
  • 5,873
  • 1
  • 19
  • 39
1
this.customerApi.getSites(this.currentUser.id)
 .subscribe(response => {
   this.sites = response;
   let pointsArray = [];
   this.sites.map((item) => {
    pointsArray.push(item.points)
   });
   // output in pointsArray
 });
ThayalanGR
  • 143
  • 2
  • 11