0

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",
  ""
 ],
]
7
  • 5
    iterating while splicing ... not a good idea. Commented Mar 22, 2018 at 13:15
  • 2
    Possible duplicate of Remove empty elements from an array in Javascript Commented Mar 22, 2018 at 13:16
  • @NinaScholz it absolutely correct. Any type of modifying of location of elements of an array, whether moving, adding, or removing, in a forEach could have undesired consequences. You would be better using a for loop 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. Commented Mar 22, 2018 at 13:19
  • What is the content of csv.data ? Commented Mar 22, 2018 at 13:19
  • 1
    That update is not that helpful, it looks now like an array of array. Could you try doing console.log(JSON.stringify(csv.data, null, " ")) and copy post that. Commented Mar 22, 2018 at 13:24

3 Answers 3

6

It's not a good idea to use splice inside the loop. You can miss some indexes. You can use filter function instead of forEach

var csv = { data: [["1212", "okay", ""], [""], [""], [""], [""], [""], [""]] };

csv.data = csv.data.filter(items => items.length > 1);

console.log(csv.data);

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

14 Comments

It will return an array with same data, but filtered., which is in the csv.data.
Please update the question and add an example of csv.data as this answer is 100% correct, for the question as it currently stands.
I've added the array and a code snippet, to show this working exactly as described.
@Archer Thanks.
@zuif - you're 100% wrong. This code works and if you just click a simple button then you'd see it. If this answer is in any way incorrect then it's because you have failed to describe the problem.
|
1

If you also wanted to remove empty elements from the top array, you could do another filter.

eg.

const a = [ [ "1212", "okay", "" ], [ "" ], [ "" ], [ "" ], [ "" ], [ "" ], [ "" ] ];

const ret1 = a.map((b) => b.filter((f) => f));
console.log(ret1);

//if you also want to remove empty elements from top array.
const ret2 = ret1.filter((f) => f.length);
console.log(ret2);

Comments

0

You could iterate the array from the end and splice without colliding with the index.

var array = [["1212", "okay", ""], [""], [""], [""], [""], [""], [""]],
    i = array.length;
    
while (i--) {
    if (array[i].length < 2) {
        array.splice(i, 1);
    }
}

console.log(array);

1 Comment

Thanks, Nina Scholz to the rescue !

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.