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.

2
  • objects use curly brackets, like this: var data = {...} Commented Jul 6, 2019 at 17:47
  • 2
    @rpivovar arrays are objects too :) Commented Jul 6, 2019 at 17:48

6 Answers 6

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)

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

3 Comments

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);}) ...
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); }
@PeterSeliger thanks for the info mate :), wasn't aware of it , fixed
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.

1 Comment

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.
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) )

1 Comment

You can safely remove length > 0 part mate you can simply use data.filter(r=>r.filter(x=>x).length)
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)

1 Comment

... 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.
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

Comments

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);

Comments

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.