What is the equivalent of Array.any? in JavaScript?

Javascript

Javascript Problem Overview


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

[].any? #=> false
[].empty? #=> true

Javascript Solutions


Solution 1 - Javascript

The JavaScript native .some() method does exactly what you're looking for:

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

[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

Solution 2 - Javascript

JavaScript has the Array.prototype.some() method:

[1, 2, 3].some((num) => num % 2 === 0);  

returns true because there's (at least) one even number in the array.

In general, the Array class in JavaScript's standard library is quite poor compared to Ruby's Enumerable. There's no isEmpty method and .some() requires that you pass in a function or you'll get an undefined is not a function error. You can define your own .isEmpty() as well as a .any() that is closer to Ruby's like this:

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

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

Libraries like underscore.js and lodash provide helper methods like these, if you're used to Ruby's collection methods, it might make sense to include them in your project.

Solution 3 - Javascript

I'm a little late to the party, but...

[].some(x => !!x)

Solution 4 - Javascript

var a = [];
a.length > 0

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

I believe this to be the cleanest and readable option:

var empty = [];
empty.some(x => x); //returns false

Solution 7 - Javascript

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

Solution 8 - Javascript

Just use Array.length:

var arr = [];

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

See MDN

Solution 9 - Javascript

What you want is .empty not .empty() to fully mimics Ruby :

     Object.defineProperty( Array.prototype, 'empty', {
           get: function ( ) { return this.length===0 }
      } );    

then

[].empty //true
[3,2,8].empty //false

For any , see my answer here

Solution 10 - Javascript

Array has a length property :

[].length // 0
[0].length // 1
[4, 8, 15, 16, 23, 42].length // 6

Solution 11 - Javascript

polyfill* :

Array.prototype.any=function(){
    return (this.some)?this.some(...arguments):this.filter(...arguments).reduce((a,b)=> a || b)
};

If you want to call it as Ruby , that it means .any not .any(), use :

Object.defineProperty( Array.prototype, 'any', {
  get: function ( ) { return (this.some)?this.some(function(e){return e}):this.filter(function(e){return e}).reduce((a,b)=> a || b) }
} ); 

__

`* : https://en.wikipedia.org/wiki/Polyfill

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkangkyuView Question on Stackoverflow
Solution 1 - JavascriptasiniyView Answer on Stackoverflow
Solution 2 - JavascriptgeorgView Answer on Stackoverflow
Solution 3 - JavascriptDudoView Answer on Stackoverflow
Solution 4 - JavascriptTGHView Answer on Stackoverflow
Solution 5 - JavascriptPointyView Answer on Stackoverflow
Solution 6 - JavascriptPedro CoelhoView Answer on Stackoverflow
Solution 7 - JavascriptAndyView Answer on Stackoverflow
Solution 8 - Javascripthaim770View Answer on Stackoverflow
Solution 9 - JavascriptAbdennour TOUMIView Answer on Stackoverflow
Solution 10 - JavascriptBenoit EsnardView Answer on Stackoverflow
Solution 11 - JavascriptAbdennour TOUMIView Answer on Stackoverflow