Ivankooo1

Towns to JSON

Sep 25th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. function solve(arr){
  2.  
  3. let data = arr.map(row => row.split('|').filter(x => x != '').map(x => x.trim()))
  4. let properties = data.shift();
  5.  
  6. let result = [];
  7.  
  8. data.forEach((row) =>{
  9. let town = {
  10. [properties[0]]: row[0],
  11. [properties[1]]: Number(Number( row[1]).toFixed(2)),
  12. [properties[2]]: Number(Number( row[2]).toFixed(2))
  13. }
  14. result.push(town);
  15. })
  16. console.log(JSON.stringify(result))
  17.  
  18. }
  19.  
  20. solve(
  21. ['| Town | Latitude | Longitude |',
  22. '| Sofia | 42.696552 | 23.32601 |',
  23. '| Beijing | 39.913818 | 116.363625 |']
  24. )
Advertisement
Add Comment
Please, Sign In to add comment