oppp

mail@pastecode.io avatar
unknown
javascript
10 days ago
1.4 kB
1
Indexable
Never
    const [csvData, setCsvData] = useState(null);

    const fetchCsvData = async ()=>{
        try {
            const response = await fetch('/assets/files/textData.csv');
            if (!response.ok) {
              throw new Error('Failed to fetch CSV data');
            }
            const csvContent = await response.text();
            const parsedData = parseCsvData(csvContent);
            console.log("The received data", parsedData['fr']['100']);
            setCsvData(parsedData);
            
        } catch (error) {
            console.error('some error lol', error);
        }
    }
    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