0

I have a variable

var test = [[[]]]

And I am wondering if there is an easy way to see if this is empty. While it's technically not empty, it is empty for my instance. Is there a way to check for this?

1
  • 3
    Can't you just check if the .length of the appropriate array is zero? Commented Jul 30, 2014 at 18:14

2 Answers 2

1

You could just deep flatten the array:

var flatten = function(xs) {
  var out = [].concat.apply([], xs)
  return xs.some(Array.isArray) ? flatten(out) : out
}

var isEmpty = function(xs) {
  return !flatten(xs).length
}

isEmpty([[[]]]) //=> true
isEmpty([[[]], [[]]]) //=> true
Sign up to request clarification or add additional context in comments.

1 Comment

beat me to it. Yay for functional.
0

Using Array.prototype.filter:

var arr = [[], [], []];
var isEmpty = arr.filter(function (arr) { return arr.length != 0; }).length === 0;
// isEmpty == true

var arr = [[], [], [1,2,3]];
var isEmpty = arr.filter(function (arr) { return arr.length != 0; }).length === 0;
// isEmpty == false

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.