Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm looking for a method for javascript returns true or false when it's empty.. something like Ruby any? or empty?

[].any? #=> false
[].empty? #=> true
share|improve this question
up vote 3 down vote accepted

Javascript standard library is quite poor compared to ruby's Enumerable, most stuff should be written by hand. On the bright side, this is trivial in most cases.

 Array.prototype.isEmpty = function() {
     return !this.length;
 }

 Array.prototype.any = function(func) {
    return this.some(func || function(x) { return x });
 }

Libraries like underscore already provide wrappers like these, if you're used to ruby's collection methods, it makes sense to include them in your project.

As @Pointy pointed out, JS arrays are not lists, they are just objects with numeric keys and length defined as max(keys) + 1. Therefore true collection methods can give surprising results (like pretty much everything else in this language).

share|improve this answer
1  
There's already .some(), which is essentially the same as .any(). – Pointy Feb 5 '15 at 17:36
2  
@Pointy: you might want to look a bit closer at how I implemented any ;) – georg Feb 5 '15 at 17:37
1  
Oh durr. Back to the coffeepot. – Pointy Feb 5 '15 at 17:38
    
Thank you for your help. Quite a high-level for me to understand. Could you please be kind and explain what func does? – kangkyu Feb 7 '15 at 23:21
    
@kangkyu: func is a function, and works like a block in ruby's any?. – georg Feb 8 '15 at 11:26

If you really want to got nuts, add a new method to the prototype:

if (!('empty' in Array.prototype)) {
  Array.prototype.empty = function () {
    return this.length === 0;
  };
}

[1, 2].empty() // false
[].empty() // true

DEMO

share|improve this answer
    
Thank you for help. I found here for Array.compact (I live in Ruby area) and they say !!(anything truthy) returns true, so it could be something like !!array.filter(function(e){ return e }).length ? – kangkyu Feb 7 '15 at 1:34
    
if (!('any?' in Array.prototype)) { Array.prototype.any? = function () {return !!this.filter(function(e){ return e }).length;};} .... I tried this didn't work urgh – kangkyu Feb 7 '15 at 1:48
    
Why don't you use georg's? Also, I don't think you can have question marks in method names. – Andy Feb 7 '15 at 2:23
1  
if (!('any' in Array.prototype)) { Array.prototype.any = function () {return !!this.filter(function(e){ return e }).length;};} this works as Array.any() Thank you Andy – kangkyu Feb 7 '15 at 23:14

JavaScript arrays can be "empty", in a sense, even if the length of the array is non-zero. For example:

var empty = new Array(10);
var howMany = empty.reduce(function(count, e) { return count + 1; }, 0);

The variable "howMany" will be set to 0, even though the array was initialized to have a length of 10.

Thus because many of the Array iteration functions only pay attention to elements of the array that have actually been assigned values, you can use something like this call to .some() to see if an array has anything actually in it:

var hasSome = empty.some(function(e) { return true; });

The callback passed to .some() will return true whenever it's called, so if the iteration mechanism finds an element of the array that's worthy of inspection, the result will be true.

share|improve this answer

Actually you are looking for javascript native some() method. From documentation:

function isBiggerThan10(element, index, array) {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
share|improve this answer
    
Thank you, I see now [12,5,8,1,4].some(function(e){ return e > 10;}); working! – kangkyu Jan 5 at 10:01

Array has a length property :

[].length // 0
[0].length // 1
[4, 8, 15, 16, 23, 42].length // 6
share|improve this answer
var a = [];
a.length > 0

I would just check the length. You could potentially wrap it in a helper method if you like.

share|improve this answer

Just use Array.length:

var arr = [];

if (arr.length)
   console.log('not empty');
else
   console.log('empty');

See MDN

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.