0

I have following array of arrays (2D Array)

Input    csvData = [["", "2", "",  ""], ["", "3", "", ""], ["", "", "4", ""]]

How to remove empty columns from above array.

   Output   csvData = [[ "2", ""], ["3", ""], ["","4"]]

I am trying but not able to complete.

  removeEmptyColumns(csvData) {
    for (let i = 0; i < csvData.length; i++) {
      let col = csvData.map(function (value, index) { return value[i]; });
      for (let j = 0; j < col.length; j++) {
        if (col[j])
          break;
      }

    }

  }
6
  • 1
    I would consider the slice method (w3schools.com/jsref/jsref_slice_array.asp) for efficiency and maintainability. Commented Mar 9, 2019 at 22:55
  • 1
    Why does the second column stays in the output matrix? Commented Mar 9, 2019 at 23:00
  • only the first cell in each row is empty. Commented Mar 9, 2019 at 23:01
  • 1
    I don't understand, shouldn't the output be: csvData = [[ "2", ""], ["3", ""], ["", "4"]]? Commented Mar 9, 2019 at 23:02
  • Yes @ibrahimmahrir .. my mistake. Commented Mar 9, 2019 at 23:22

2 Answers 2

5

You could get first the filled column and then filter the arrays.

var csvData = [["", "2", "",  ""], ["", "3", "", ""], ["", "", "4", ""]],
    columns = csvData.reduce(
        (r, a) => (a.forEach((v, i) => r[i] = r[i] || v), r),
        []
    );

csvData = csvData.map(a => a.filter((_, i) => columns[i]));

console.log(csvData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

For deleting only the last empty columns, you could get the max value of the filled columns and delete the rest.

var csvData = [["", "2", "",  ""], ["", "3", "", ""], ["", "", "4", ""]],
    max = csvData.reduce(
        (r, a) => (a.forEach((v, i) => v && (r = Math.max(r, i))), r),
        0
    );

csvData = csvData.map(a => a.slice(0, max + 1));

console.log(csvData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

What is the time complexity for the above answer? Is there any edge cases it can fail?
at least O(2n).
could you please solve my another complex problem. stackoverflow.com/questions/55083788/…
2

Use Array.reduce() with Array.some() to identify empty columns.

Then use Array.map() to iterate the array, and Array.filter() to remove the items at the empty columns.

const removeEmptyColumns = arr => {
  // detect empty columns
  const emptyColumns = (arr[0] || []).map((c, i) => arr.some(a => a[i]))
  
  // filter empty columns
  return arr.map(a => a.filter((_, i) => emptyColumns[i]))
}

pp(removeEmptyColumns([["", "2", "",  ""], ["", "3", "", ""], ["", "", "4", ""]]))

pp(removeEmptyColumns( [["1", "2", "", "4"], ["", "3", "", ""], ["", "", "", "2"]]
))

function pp(d) {
  console.log(JSON.stringify(d))
}

1 Comment

Droi, Could you please try to solve my another complex problem stackoverflow.com/questions/55083788/…

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.