Home > Mobile >  Making an array according to a certain template
Making an array according to a certain template

Time:09-17

I'm using google maps in my app. There is a problem about markers positions. The google want to datas like;

const stops = [
            [{ lat: 34.8791806, lng: -111.8265049 }, "Boynton Pass", 1],
            [{ lat: 34.8559195, lng: -111.7988186 }, "Airport Mesa", 2],
            [{ lat: 34.832149, lng: -111.7695277 }, "Chapel of the Holy Cross", 3],
            [{ lat: 34.823736, lng: -111.8001857 }, "Red Rock Crossing", 4],
            [{ lat: 34.800326, lng: -111.7665047 }, "Bell Rock", 5],
          ];

and my result data getting from api;

[{"id":40,"name":"3C Cinnah Cafe","longitude":32.855826,"latitude":39.896092},{"id":49,"name":"A.K. Simit Fırın Cafe","longitude":32.8469142,"latitude":39.8871573},{"id":61,"name":"Aroma Coffee","longitude":32.7981522,"latitude":39.8943212}]

So I just want set data according the google template. Any suggestion?

CodePudding user response:

You can use the map function to map the array you receive to the format you want.

const apiData = [
  { id: 40, name: "3C Cinnah Cafe", longitude: 32.855826, latitude: 39.896092 },
  {
    id: 49,
    name: "A.K. Simit Fırın Cafe",
    longitude: 32.8469142,
    latitude: 39.8871573,
  },
  { id: 61, name: "Aroma Coffee", longitude: 32.7981522, latitude: 39.8943212 },
];

const formattedData = apiData.map((datum, i) => [
  {
    lat: datum.latitude,
    long: datum.longitude,
  },
  datum.name,
  i   1,
]);

console.log("formattedData", formattedData);

  • Related