In operator returns false [duplicate] - javascript

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.

Related

Checking for the existence of a javascript var inside an array that may or may not exist [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 16 days ago.
I'm trying to check to see if something exists in an array, let's say the array arr is:
{"targeting":{
"key1":"1",
"key2":"2"},
"session": "1234"
}
I want to check to see if arr.targeting.key1 exists so I do:
if (true === !!arr.targeting.key1)
This is true if it exists, but what if arr is empty? Then I get an error:
cannot read properties of undefined, reading 'key1'
What is the simplest way to check for the presence of arr.targeting.key1 that will just return false if arr.targeting or arr themselves are undefined?
Use optional chaining.
if (arr?.targeting?.key1)

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.

Why can we use the .length property in string types? [duplicate]

This question already has answers here:
How does primitive types in Javascript have methods and Properties? [duplicate]
(2 answers)
Closed last year.
Hello I'm new to JS and object-oriented programming in general.
I have two questions considering this
let arr = [1,2,3]
let a = 'hi'
When I run typeof(), arr is 'object' and a is 'string' right?
So my question is ,
When using arr.length to get the length of the array, what's the principle behind it? To be specific, I don't understand how I could get the length property though I've never initialized it. Does JS automatically set a length property value when we generate an object? How does it work?
Doesn't property only exist in objects? But why can we get the length of variable a using a.length? I thought objectname.property thing was for objects.
When you declare [1,2,...] you are declaring an object of class Array, which, in it's prototype, has a "length" property. Think of it as a variable that gets updated when you add or remove elements from the array.
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/length
Strings are also objects in Javascript, and a string is also considered an array of characters, thus having the "length" property as well.
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

Test whether every element of array is truthy [duplicate]

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.

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