read a csv file, concatenated the first two columns with a dash, and then compare to a different csv file, removing any rows from it that start with the concatenated value. only standard modules in javascript

Assuming that the CSV files have headers and are not too large, we can use the following steps to read and process them:

  1. Read the first CSV file line by line using fetch API and parse it using papaparse library.
  2. Concatenate the first two columns with a dash using array.map and Array.join methods.
  3. Read the second CSV file line by line using fetch API and parse it using papaparse library.
  4. Filter out any rows from the second CSV file that start with the concatenated value using Array.filter method.
  5. Convert the filtered rows back to CSV format using papaparse library.

Here's the code:

index.tsx
// Import papaparse library (https://www.papaparse.com/)
import Papa from 'papaparse';

// URL of first CSV file
const url1 = 'https://example.com/csv1.csv';

// URL of second CSV file
const url2 = 'https://example.com/csv2.csv';

// Read first CSV file and parse it
fetch(url1)
  .then(response => response.text())
  .then(csv1 => {
    const data1 = Papa.parse(csv1, { header: true }).data;
    
    // Concatenate first two columns with a dash
    const newdata1 = data1.map(row => [row.column1, row.column2].join('-'));

    // Read second CSV file and parse it
    fetch(url2)
      .then(response => response.text())
      .then(csv2 => {
        const data2 = Papa.parse(csv2, { header: true }).data;

        // Filter out rows that start with concatenated value
        const filteredData2 = data2.filter(row => {
          const value = [row.column1, row.column2].join('-');
          return !newdata1.includes(value);
        });

        // Convert filtered rows back to CSV format
        const csv = Papa.unparse(filteredData2, { header: true });
        console.log(csv);
      });
  });
1103 chars
36 lines

Note that this code uses Promise and arrow functions syntax.

related categories

gistlibby LogSnag