Advertisement
IvaAnd

7.Towns to JSON

Oct 3rd, 2021
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function json(input) {
  2.  
  3.     let [columns, ...table] = input.map(splitLine);
  4.  
  5.     return JSON.stringify(table.map(entry => {
  6.         return columns.reduce((acc, curr, index) => {
  7.             acc[curr] = entry[index];
  8.             return acc;
  9.         }, {})
  10.  
  11.     }));
  12.  
  13.  
  14.     function splitLine(input) {
  15.         return input.split('|')
  16.             .filter(emptyString)
  17.             .map(x => x.trim())
  18.             .map(convertIfNum)
  19.     }
  20.  
  21.     function emptyString(str) {
  22.         return str !== '';
  23.     };
  24.  
  25.     function convertIfNum(num) {
  26.         return isNaN(num) ? num : Number(Number(num).toFixed(2));
  27.     };
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement