Is there an equivalent to Python's all function in JavaScript or jQuery?

Khaelid

In Python the all() functions tests if all values in a list are true. For example, I can write

if all(x < 5 for x in [1, 2, 3, 4]):
    print("This always happens")
else:
    print("This never happens")

Is there an equivalent function in JavaScript or jQuery?

Cerbrus

Apparently, it does exist: Array.prototype.every. Example from mdn:

function isBigEnough(element, index, array) {
    return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true

This means you won't have to write it manually. This function doesn't work on IE8-, though.

However, if you want a function that also works for IE8-, you can use a manual implementation, Or the polyfill shown on the mdn page.

Manual:

function all(array, condition){
    for(var i = 0; i < array.length; i++){
        if(!condition(array[i])){
            return false;
        }
    }
    return true;
}

Usage:

all([1, 2, 3, 4], function(e){return e < 3}) // false
all([1, 2, 3, 4], function(e){return e > 0}) // true

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the equivalent of jQuery's .before() function in Javascript?

From Dev

What is the equivalent of jQuery's .before() function in Javascript?

From Dev

Javascript equivalent function for jquery .on() function

From Dev

Equivalent function in Javascript from jQuery

From Dev

JavaScript equivalent of Python's parameterised string.format() function

From Dev

Is there a JavaScript equivalent to Python's for loops?

From Dev

What is the equivalent of python's _ in javascript?

From Dev

Python's equivalent for R's dput() function

From Dev

Equivalent function in JavaScript/Ramda as Clojure's juxt

From Dev

Equivalent function in JavaScript/Ramda as Clojure's juxt

From Java

Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

From Dev

What is the JavaScript equivalent of jQuery's hide() and show()?

From Dev

What is the JavaScript equivalent of jQuery's hide() and show()?

From Dev

What is the bash equivalent of Python's all?

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

Python equivalent of R's head and tail function

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

Is there a Python equivalent to MATLAB's pearsrnd function?

From Dev

What is the Java equivalent to Python's reduce function?

From Dev

Pandas equivalent of Python's readlines function

From Dev

Is there an equivalent to Python's exec() function in Java?

From Dev

Equivalent of Python's dir function in PHP

From Dev

Is there a python ReportLab equivalent of TCPDF's 'annotate' function?

From Dev

Is there a C# equivalent to python's type() function?

From Dev

Python equivalent to Matlab's set function

From Dev

What is the Python Equivalent of Batch's PAUSE function

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

jQuery function as a string with arguments as Array (JavaScript "apply()" equivalent for jQuery elements)

From Dev

Does python have an equivalent to Javascript's 'btoa'

Related Related

  1. 1

    What is the equivalent of jQuery's .before() function in Javascript?

  2. 2

    What is the equivalent of jQuery's .before() function in Javascript?

  3. 3

    Javascript equivalent function for jquery .on() function

  4. 4

    Equivalent function in Javascript from jQuery

  5. 5

    JavaScript equivalent of Python's parameterised string.format() function

  6. 6

    Is there a JavaScript equivalent to Python's for loops?

  7. 7

    What is the equivalent of python's _ in javascript?

  8. 8

    Python's equivalent for R's dput() function

  9. 9

    Equivalent function in JavaScript/Ramda as Clojure's juxt

  10. 10

    Equivalent function in JavaScript/Ramda as Clojure's juxt

  11. 11

    Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

  12. 12

    What is the JavaScript equivalent of jQuery's hide() and show()?

  13. 13

    What is the JavaScript equivalent of jQuery's hide() and show()?

  14. 14

    What is the bash equivalent of Python's all?

  15. 15

    Is there a Python equivalent to R's sample() function?

  16. 16

    Python equivalent of R's head and tail function

  17. 17

    Matlab equivalent of Python's 'reduce' function

  18. 18

    Is there a Python equivalent to MATLAB's pearsrnd function?

  19. 19

    What is the Java equivalent to Python's reduce function?

  20. 20

    Pandas equivalent of Python's readlines function

  21. 21

    Is there an equivalent to Python's exec() function in Java?

  22. 22

    Equivalent of Python's dir function in PHP

  23. 23

    Is there a python ReportLab equivalent of TCPDF's 'annotate' function?

  24. 24

    Is there a C# equivalent to python's type() function?

  25. 25

    Python equivalent to Matlab's set function

  26. 26

    What is the Python Equivalent of Batch's PAUSE function

  27. 27

    Matlab equivalent of Python's 'reduce' function

  28. 28

    jQuery function as a string with arguments as Array (JavaScript "apply()" equivalent for jQuery elements)

  29. 29

    Does python have an equivalent to Javascript's 'btoa'

HotTag

Archive