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?
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.