1

I have such an object

var data = [[1,2,34,4,5,6],[3,34,5,3,42,4],[,,,,,],[,,,],[,,,,,],[1,1]]

I need to filter it and get such a result:

var data = [[1,2,34,4,5,6],[3,34,5,3,42,4],[1,1]]

I tried to do it with array.filter() but I can't get the right expression.

GhostKU
  • 1,898
  • 6
  • 23
  • 32

6 Answers6

2
 const result = array.map(it => it.filter(_ => true)).filter(sub => sub.length)

First of all, remove all the empty slots from the inner arrays, then remove all arrays with length 0.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    I would consider marking this one as the most helpful answer since it's approach works for both edge cases, entirely empty arrays and partially sparse ones too which will be condensed into non-sparse arrays. – Peter Seliger Jul 06 '19 at 21:04
2

Using filter and some

let a = [[1,2,34,4,5,6],[3,34,5,3,42,4],[,,,,,],[,,,],[,,,,,],[1,1]]

let final = a.filter(val=> val.some(v => true))

console.log(final)
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 2
    1/2 ... The test for an empty array is not correct. It only accidentally works with the given example ... `[,,,,,].every(v => v === undefined)`. Since the given array literal by definition ***is*** an entirely sparse array (thus empty), it gets not iterated at all ... but the method `every` by definition also returns the `true` value for any empt array without running its provided callback function even once. Compare (a) `[,,,,,].every(v => {console.log(v); return (v === undefined);})` with (b) `[,,undefined,,,].every(v => {console.log(v); return (v === undefined);})` ... – Peter Seliger Jul 06 '19 at 21:15
  • 2
    2/2 ...(a) does not log at all since it processes an entirely sparse array, but (b) does because the operated array is not entirely sparse. The test for an empty array then does fail because of the wrong condition provided with the callback function ... after all, `[,,undefined,,,]` is not an empty array. It explicitly holds an `undefined` value at index 2 ... `2 in [,,undefined,,,]` is `true`, whereas it is `false` for any other index in between 0 and 4. Thus a valid test for real array emptiness should be based on `some` ... e.g.: `function isEmptyArray(arr) {return !arr.some(o => true); }` – Peter Seliger Jul 06 '19 at 21:17
  • 1
    @PeterSeliger thanks for the info mate :), wasn't aware of it , fixed – Code Maniac Jul 07 '19 at 02:41
0

it a double filter case: ( and the shortest code ) ;)

var  data = [[1,2,34,4,5,6],[3,34,5,3,42,4],[,,,,,],[,,,],[,,,,,],[1,1]]

var R = data.filter(r=>r.filter(x=>x).length)

console.log( JSON.stringify(R) )
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
0

Here you have a more declarative example. Hope it helps:

  • create a function isArrayEmpty(arr) that returns a boolean that is true if an array is empty. To go deeper into detecting empty values in an array (not just falsy) see this stackoverflow thread. Also see Peter's comment.
  • with this function, filter data excluding empty subarrays.

var data = [[1,2,34,4,5,6],[3,34,5,3,42,4],[,,,,,],[,,,],[,,,,,],[1,1]]

var isArrayEmpty = arr => !arr.filter(elem => true).length;

var result = data.filter(subarray => !isArrayEmpty(subarray))

console.log(result)
Nico Diz
  • 1,484
  • 1
  • 7
  • 20
  • 1
    ... the implementation of `isArrayEmpty` might be reconsidered ... compare your's `isArrayEmpty([,,,undefined,,,,])` with this approach ... `function isEmptyArray(arr) {return !arr.some(o => true); }` ... and `isEmptyArray([,,,undefined,,,,])` ... see my 2 cents to @code-maniac 's answer above. – Peter Seliger Jul 06 '19 at 20:40
0

let arr = [[1],[1,2],[,2],[,,],,[]]

console.log(JSON.stringify( arr.filter(a => 0 in a) ))

console.log(JSON.stringify( arr.filter(a => Object.keys(a).length) )) // to include sparse subarrays
Slai
  • 22,144
  • 5
  • 45
  • 53
0

Another alternative could be use reduce and some.

const data = [[1,2,34,4,5,6],[3,34,5,3,42,4],[,,,,,],[,,,],[,,,,,],[1,1]];

const filteredData = data.reduce((accumulator, currentValue) => {
  if(currentValue.some(x => x)){
    accumulator.push(currentValue);
  }
  return accumulator;
}, []);

console.log(filteredData);
rcoro
  • 326
  • 6
  • 12