Advertisement
KoctrX

Untitled

Nov 24th, 2023
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _parseCSVtoJSONByN(input) {
  2.         const lines = input.split('\n');
  3.         const parsedData = {};
  4.  
  5.         lines.forEach(line => {
  6.             if (line.trim() === '') return;
  7.             let [key, value] = line.split(': ');
  8.             const match = key.match(/_(\d+)$/);
  9.             if (match) {
  10.                 const index = match[1];
  11.                 key = key.replace(/_\d+$/, '');
  12.  
  13.                 if (!parsedData[index]) {
  14.                     parsedData[index] = {};
  15.                 }
  16.  
  17.                 parsedData[index][key] = value;
  18.             }
  19.         });
  20.  
  21.         return Object.values(parsedData).map(data => { return data; });
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement