Advertisement
TZinovieva

Towns to JSON JS Advanced

Jun 13th, 2023
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function townsToJSON(input) {
  2.     let result = [];
  3.     for (let i = 1; i < input.length; i++) {
  4.         let line = input[i];
  5.         let row = line.split('|');
  6.         row.pop();
  7.         row.shift();
  8.         row[0] = row[0].trim();
  9.         row[1] = row[1].trim();
  10.         row[2] = row[2].trim();
  11.  
  12.         let [town, latitude, longitude] = row;
  13.         latitude = Number(latitude).toFixed(2);
  14.         longitude = Number(longitude).toFixed(2);
  15.  
  16.         result.push({ Town: town, Latitude: Number(latitude), Longitude: Number(longitude) });
  17.     }
  18.     console.log(JSON.stringify(result));
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement