Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't make array of arrays

I’m trying to make an array of coordinate arrays, but I’m blanking out on how to doing. How do I make this happen?

Code –

targetLocations() {
  const data = []
  const timer = setInterval(() => {
    for (let i = 0; i<store.state.location.locations.length; i++) {
      const coordinates = [store.state.location.locations[i]["longitude"]+","+store.state.location.locations[i]["latitude"]]
      data.push(coordinates)
      console.log("this is the data "+data)
    }
  }, 1000);
},

I’ve tried making coordinates an array and it still didn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Based on the provided code, it seems that you are trying to create an array of coordinate arrays by iterating through an array of locations and pushing the corresponding coordinates into the data array.

The approach you are taking looks correct, however, you can simplify the code a bit by using Array.map() function which returns a new array with the results of calling a provided function on every element in the calling array. Here’s an example of how you can modify your code to achieve the same result using Array.map():

targetLocations() {
  const data = store.state.location.locations.map(location => [location.longitude, location.latitude]);
  console.log("this is the data", data);

  const timer = setInterval(() => {
    // do something with data array
  }, 1000);
}

In the modified code, the Array.map() function is used to iterate through the store.state.location.locations array and return a new array containing the corresponding coordinates for each location. The coordinates are returned as an array containing the longitude and latitude values in the same order as they appear in the original data.

After creating the data array, it is logged to the console for debugging purposes. Then, the setInterval() function is called to perform some operation using the data array at regular intervals.

I hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading