I am trying to remove the empty array elements (from a csv file) by using the splice() function.
The array elements are stored inside csv.data:
csv.data.forEach(function(item,index) {
if (item.length < 2) { // don't want anything less than two
csv.data.splice(index,1);
}
});
This works, but it still returns me two empty arrays (lines) in the csv file, originally, there are six empty lines, but it skips the two empty lines.
Am I doing anything wrong?
This is the csv.data
[
[
"1212",
"okay",
""
],
[
""
],
[
""
],
[
""
],
[
""
],
[
""
],
[
""
]
]
Expected
[
[
"1212",
"okay",
""
],
]
forloop and adding logic to keep track of where you were (but only as a fun way to understand the logic and why it breaks). But, that's also not the best solution here. I just thought I would reiterate why these modifications are not recommended in a forEach.csv.data?console.log(JSON.stringify(csv.data, null, " "))and copy post that.