Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of python any() and all() functions in JavaScript?

Python has built in functions any() and all(), which are applied on a list (array in JavaScript) as following-

  • any(): Return True if any element of the iterable is true. If the iterable is empty, return False.
  • all(): Return True if all elements of the iterable are true (or if the iterable is empty).

We can create our customized functions for above, but please let me know if there any equivalent built-in functions available in JavaScript.

like image 774
Workonphp Avatar asked May 15 '14 09:05

Workonphp


People also ask

What are any () and all () function?

The Python any() and all() functions evaluate the items in a list to see which are true. The any() method returns true if any of the list items are true, and the all() function returns true if all the list items are true.

What is a any () in Python?

Python any() Function The any() function returns True if any item in an iterable are true, otherwise it returns False. If the iterable object is empty, the any() function will return False.

How do you use a ANY () or a all () in Python?

any() is a function that takes in an iterable (such as a list, tuple, set, etc.) and returns True if any of the elements evaluate to True , but it returns False if all elements evaluate to False . You can also check the documentation of the any() function by using help() , as shown below.

What does any mean in JavaScript?

Very simple and straightforward answer is function(): any will return any type of data. It means you can return string boolean number or any type of data you want from that function. Follow this answer to receive notifications. answered Apr 12 at 2:42.


3 Answers

The Python documentation gives you pure-python equivalents for both functions; they are trivial to translate to JavaScript:

function any(iterable) {     for (var index = 0; index < iterable.length; index++) {         if (iterable[index]) return true;     }     return false; } 

and

function all(iterable) {     for (var index = 0; index < iterable.length; index++) {         if (!iterable[index]) return false;     }     return true; } 

Recent browser versions (implementing ECMAScript 5.1, Firefox 1.5+, Chrome, Edge 12+ and IE 9) have native support in the form of Array.some and Array.every; these take a callback that determines if something is 'true' or not:

some_array.some((elem) => !!elem ); some_array.every((elem) => !!elem ); 

The Mozilla documentation I linked to has polyfills included to recreate these two methods in other JS implementations.

like image 164
Martijn Pieters Avatar answered Oct 10 '22 07:10

Martijn Pieters


You can use lodash.

lodash.every is equivalent to all

lodash.some is equivalent to any

like image 33
the9ull Avatar answered Oct 10 '22 07:10

the9ull


Build-in function some is equivalent to any I suppose.

const array = [1, 2, 3, 4, 5];

const even = function(element) {
  // checks whether an element is even
  return element % 2 === 0;
};

console.log(array.some(even));
// expected output: true

You can read more in the docs

like image 43
Developerium Avatar answered Oct 10 '22 05:10

Developerium