0

I am trying to look for any simple way or any node packages which provides this functionality.

My csv data is as following:

"date","time1","height1","time2","height2","time3","height3","time4","height4"
"30 Ogos 2021","05:08","175","11:30","93","17:00","148","23:04","88"
"31 Ogos 2021","06:00","160","12:39","104","18:40","132",,
"30 Ogos 2021","05:08","175","11:30","93","17:00","148","23:04","88"
"31 Ogos 2021","06:00","160","12:39","104","18:40","132",,

Different row will have dynamic number of columns.

2
  • To remove duplicates based on which column ? Commented Aug 30, 2021 at 7:57
  • I plan to remove duplicate rows, so its all columns. Commented Aug 30, 2021 at 8:01

1 Answer 1

2

This solution is inefficient, I believe it's O(n^2), but it will work, if we assume that your CSV is represented in javascript as a 2D array of strings (i.e. string[][] in typescript notation).

const table = loadCsv(...); // this line is pseudocode

/**
 * Check if two arrays are the same
 */
function rowCompare(rowA, rowB) {
  if (rowA.length !== rowB.length) return false;
  for (let i = 0; i < rowA.length; i++) {
    if (rowA[i] !== rowB[i]) return false;
  }
  return true;
}

const dedupedTable = table.filter((row, ind, arr) => {
  return ind === arr.findIndex(r => rowCompare(row, r))
});

The dedupedTable variable should now contain your table, with any duplicate rows removed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.