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

Why are split and join results are different?

I used split and join to swap latitude and longitude of the coordinates. I am using leaflet to get the coordinates and as polyline, swapping coordinates works just fine but when I do it using marker, the last digit of the latitude and first digit of longitude is gone
This is the code, as you can see they’re just the same code

if (type === 'polyline') {
    const res = ["[" +
      tmp.map(
        x => '[' +
        swapLatLon(
          stripFirstLast(x).split(', ')
        ).join(', ') +
        ']'
      ).join(', ') +
      "]"
    ];
  document.getElementById("polyline").value = res;
} else if (type === 'marker') {
    const res = ["[" +
      tmp.map(
        x => '[' +
        swapLatLon(
          stripFirstLast(x).split(', ')
        ).join(', ') +
        ']'
      ).join(', ') +
      "]"
    ];
  document.getElementById("marker").value = res;
} else {
  console.log('__undefined__');
}

This is a sample result for res

Original: [37.0902, 95.7129]
Expectation: [[95.7129, 37.0902]]
Reality: [[95.712, 7.0902]]

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 :

If you only want to swap places of two variables in an array, then use deconstructing.

const tmp = [37.0902, 95.7129]
let [long, lat] = tmp;
let res = [[lat, long]];
let type;

if (type === 'polyline') {
  document.getElementById("polyline").value = res;
} else if (type === 'marker') {
  document.getElementById("marker").value = res;
} else {
  console.log('__undefined__');
}

console.log({tmp})
console.log({res})
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