Test whether every element of array is truthy [duplicate] - javascript

This question already has answers here:
How to return true if all values of array are true otherwise return false?
(4 answers)
Closed 2 years ago.
Given an array a, what is the simplest way to produce a boolean value that is true iff every value in a is truthy?
EDIT:
Is it a.every(i => i)?

You can use Array.every, which checks every element in the array you call it on and returns true if every item matches the condition you pass it as an argument. For your case, you can pass it a brief lambda function:
myArray.every(i => i)
Or simply use the Boolean constructor, which will straight away make whatever you pass it into a bool.
myArray.every(Boolean)
Therefore every item in the array has Boolean(item) called upon it, and if every one of them returns true then .every() will return true as well.

Related

In JS, when using or (||) in if statement, when first is true, does it evaluated second? JavaScript behavior [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
In javascript, if (true || false) results in true, but the question is whether the compiler will evaluate the second statement if the first is true.
In my case, I want to know if an array has changed, so I believe I have two options: compare the lengths of the arrays (what was and what is now) and compare if the array values are different.
I think the first option requires less work.
If (first || second) { give me deleted values, and give me added values }
No, it does not
first() || second()
function first() {
console.log('first')
return true
}
function second() {
console.log('second')
return false
}

indexOf not working correctly in javaScript [duplicate]

This question already has answers here:
Difference Between indexOf and findIndex function of array
(9 answers)
Closed 4 months ago.
my code is :
[{a:1},{b:2},{c:3}].indexOf(obj=>{return obj.a ==1})
I expect return 0 but result is -1
what is the problem?
The main difference are the parameters of these functions:
Array.prototype.indexOf() expects a value as first parameter. This
makes it a good choice to find the index in arrays of primitive types
(like string, number, or boolean).
Array.prototype.findIndex() expects a callback as first parameter.
Use this if you need the index in arrays with non-primitive types
(e.g. objects) or your find condition is more complex than just a
value.
This question is similar to this and the answer can also be found there.

In operator returns false [duplicate]

This question already has answers here:
javascript string in list returns false
(3 answers)
Closed 3 years ago.
Why this statement returns false? It's really weird
console.log("100038916831294" in ["100003748210938", "100038916831294"]);
The in operator tells you whether a value exists as a property name in an object. The property names of your array are "0" and "1".
You can use one of the Array methods to check if a value is in the array, like .indexOf() or .includes():
console.log(["100003748210938", "100038916831294"].includes("100038916831294"));
The in operator in JavaScript compares indexes or property names in arrays instead of the value itself.
For example, if we write console.log(0 in ["abc","pqr"]); it will print true. However, if we use the value, like in console.log("abc" in ["abc","pqr"]); it will print false.
You can further read about it on https://www.w3schools.com/jsref/jsref_operators.asp.

Calling `every` and `some` methods of an empty array returns weird results [duplicate]

This question already has answers here:
Why does Array.prototype.every return true on an empty array?
(4 answers)
Closed 6 years ago.
In JavaScript why does the line [].every(Boolean) return true? And [].some(Boolean) returns false?
If there are no elements in the array, then they are kind of undefined (undeclared), thus falsy. According to this, snippets with arrays [null] or [undefined], return false exactly as predicted.
But any empty array should not call the callback Boolean at the first place, and return something like undefined or null, thus false again.
What I've missed?
It looks like, what JS interpreter really does is that Boolean([]) (in the first situation). And that of course returns true. Maybe that is correct?
"every" returns true if every element of an array passes a test. If there are no items in the array, "every" element in the array passes the test.
"some" returns true if at least one element of an array passes a test. If the array is empty, none of the elements pass the test and it returns false.
It looks like, what JS interpreter really does is that Boolean([]) (in the first situation). And that of course returns true. Maybe that is correct?
With an empty array, neither every nor some call the callback at all because there is nothing to test. You can check this with:
[].every(() => console.log("this never prints"));
[].some(() => console.log("this never prints"));

How to check the return value of the first function in jQuery [duplicate]

This question already has answers here:
How do you check if a selector matches something in jQuery? [duplicate]
(11 answers)
Closed 7 years ago.
Why does the following code return an empty array as a result ([])?
$('#non-existing-id').first();
I thought that it should return null or undefined.
How can I check for the success then? I don't see anything about it in the documentation.
Why does the following code return an empty array
It doesn't. It returns a jQuery object containing only the first match.
If there are no matches, that jQuery object contains zero elements.
I thought that it should return null or undefined.
No, the documentation says it returns a jQuery object.
How can I check for the success then?
Test the number of matches using length.
if ($('#non-existing-id').length > 0)
$('#non-existing-id') returns an empty array since it found zero matches.
.first() returns zero results since it doesn't find any result in an empty array.
$('#non-existing-id').eq(423424); also returns an empty array [] as example.

Categories

Resources