3

I have a two dimensional array where the last elements are empty. Like this:

data = [["1","2","3","","",""],["4","5","6","x","f",""],["7","8","9","",""]]

Now I want to remove the empty elements and get the correct new size with .length

I tryed it like this but it doesn't work:

        for (i = 0; i != data[2].length-1; i++) {
            if (data[2][i].isEmpty()) {
                data[2] = data[2].splice(i, 1);
            }
        }

Is there any good solution?

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
R98
  • 109
  • 1
  • 9
  • Take a look at [pop()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) – Herohtar Jan 22 '18 at 22:33
  • What is your desired output? Is it `data = [["1","2","3"],["4","5","6","x","f"],["7","8","9"]]`? – Joseph Marikle Jan 22 '18 at 22:34
  • Compare them to the empty string (i.e., `=== ""`), or check their length (i.e., `.length === 0`). There is no such thing as `isEmpty()` unless you've built it yourself. – Heretic Monkey Jan 22 '18 at 22:34
  • 1
    Coincidentally, all the empty elements are the last ones. But if you just want to remove empties (not necessarily the last), then maybe: `noEmptys = data.map(subarray => subarray.filter(Boolean)).filter(arr => arr.length)`? – CRice Jan 22 '18 at 22:35
  • @JosephMarikle yes exactly thats the output i want and data[2].length should be 5 – R98 Jan 22 '18 at 22:39
  • 1
    @R98 You want `data[2].length` to be `5` even though it only has 3 elements?? Or did you mean `data[1].length`? – CRice Jan 22 '18 at 22:41
  • @CRice yeah you are right i meant data[1] don't know how i could mess this up – R98 Jan 22 '18 at 22:54

5 Answers5

6

You could map the arrays after filtering with Boolean as callback for keeping truthy elements.

var data = [["1", "2", "3", "", "", ""], ["4", "5", "6", "x", "f", ""], ["7", "8", "9", "", ""]];

data = data.map(a => a.filter(Boolean));

console.log(data);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Using [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) is very clever. – ryanpcmcquen Jan 22 '18 at 22:44
  • thanks works perfectly for the whole array but don't work for me if i try to use only an inner array like data[2] = data[2].map(a => a.filter(Boolean)) – R98 Jan 22 '18 at 22:52
  • 1
    @R98, then just filter the array, like `data[2] = data[2].filter(Boolean);` without mapping, because you have only one array for filtering. – Nina Scholz Jan 23 '18 at 09:19
2

Splicing in a for() loop is tricky due to changing the lengths of the arrays

Use map() and filter()

let data = [["1","2","3","","",""],["4","5","6","x","f",""],["7","8","9","",""]]

data = data.map(arr => arr.filter(e => e.trim()));  

console.log(JSON.stringify(data))
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • works totally fine for the whole array how can i use it for only one array inside data? for example data[1]?because it sadly doesnt work with that – R98 Jan 22 '18 at 23:04
  • Just use `filter` on that array. map is used to do all of them – charlietfl Jan 23 '18 at 00:13
1

Use .map and .filter:

const data = [
  ['1', '2', '3', '', '', ''],
  ['4', '5', '6', 'x', 'f', ''],
  ['7', '8', '9', '', ''],
];

const cleanData = data.map(arr => arr.filter(value => value || false));

console.log(cleanData);
ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37
0

Arrow functions are elegant, but not fully supported at the moment, so probably following may be better solution:

const data = [
    ['1', '2', '3', '', '', ''],
    ['4', '5', '6', 'x', 'f', ''],
    ['7', '8', '9', '', ''],
];

const filteredData = data.map(function (arr) {
    return arr.filter(Boolean);
});


console.log(filteredData);
soeik
  • 905
  • 6
  • 15
0
var data = [["1","2","3","","",""],["4","5","6","x","f",""],["7","8","9","",""]];
var i;

for(i = 0; i < data[2].length; i++){
  if(data[2][i] == '')
  {
    var temp = data[2].slice(0,i);
    break;
  }
}

console.log(temp);

If you want to keep the same data structure and the pattern that each array will have a series of empty strings at the end this will find the first empty string and chop off the rest.

wiz
  • 1
  • 1
  • 2