optional

mail@pastecode.io avatar
unknown
javascript
9 days ago
753 B
1
Indexable
Never
    const parseCsvData = (csvContent) => {
        const rows = csvContent.split('\n');
        const headers = rows[0]?.split(',') || [];
        const data = {};
        for (let i = 1; i < headers.length; i++) {
          const lang = headers[i]?.trim(); 
          if (lang) {
            data[lang] = {};
          }
        }
        for (let i = 1; i < rows.length; i++) {
          const row = rows[i]?.split(',') || [];
          const key = row[0]?.trim(); 
          for (let j = 1; j < headers.length; j++) {
            const lang = headers[j]?.trim(); 
            const value = row[j]?.trim();
            if (key && value && lang) {
              data[lang][key] = value;
            }
          }
        }
        return data;
      };
Leave a Comment