Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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]]

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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})

about 4 years ago · Santiago Trujillo Report

0

split and join are inverses of each other. It is likely that the issue is elsewhere in the data processing.

In any case, here is a direct one line solution using the data examples that you provided:

x = "[37.0902, 95.7129]"
swapped = "[" + x.slice(1,-1).split(", ").reverse().join(", ") + "]"
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!