Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does lodash `_.all([true, true, true], true);` return `false`?

How can I check if all elements of an array are truthy or falsey.

Since the following doesn't seem to do it: _.all([true, true, true], true);

it returns: false?

like image 215
Victor S Avatar asked Jun 17 '15 19:06

Victor S


People also ask

Why does true == true return false?

Because they don't represent equally convertible types/values. The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true') . So given this code true == 'true' , it finds this: "If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y ."

What does Lodash find return?

Lodash's find() function returns the first element of a collection that matches the given predicate .

How does Lodash get work?

get() method in Lodash retrieves the object's value at a specific path. If the value is not present at the object's specific path, it will be resolved as undefined . This method will return the default value if specified in such a case.

Is false Lodash?

falsey() Method. The Lodash _. falsey() method checks whether the given value is falsey or not and returns the corresponding boolean value. A falsey value is one that means false in a boolean context.


1 Answers

You should re-read the _.every(collection, [predicate=_.identity]) api doc of lodash. The issue with your code is the second param you are passing. Remove it and it works

> _.every([true, 'foo', 1])
true
> _.every([true, 'foo', 1, 0])
false
like image 135
Sebastian Dominguez Avatar answered Sep 19 '22 09:09

Sebastian Dominguez