Javascript "in" give wrong results [duplicate] - javascript

This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 2 years ago.
I have the following code in Javascript:
var myArray = {"a":21600904, "b":21100999, "c":21602019, "d":21704354, "e":21602271, "f":21500123};
console.log(Object.values(myArray));
console.log(21600904 in Object.values(myArray));
And I get the following output:
[ 21600904, 21100999, 21602019, 21704354, 21602271, 21500123 ]
false
But this was not what I expected. What I understand is, 21600904 is inside Object.values(myArray) but 21600904 in Object.values(myArray) returns false instead of true.
What am I missing here? Why this code does not print true?

Related

"in" keyword not working properly when used with object.value in node [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 1 year ago.
I need to execute some code. But my code is not working properly. I have made an simple example to demonstrate the malfunction.
When I run
console.log("val" in Object.values({key:"val"})); //returns false
It gives me false. But if I run
console.log(Object.values({key:"val"}))
outputs => ['val']
I don't understand if it is supposed to work like this. If yes. Why ?
Thanks in Advance.....:)
MDN says "the in operator returns true if the specified property is in the specified object or its prototype chain.", Object.values returns an array. To check if an item exists in an array, use the Array.includes method.
console.log(
Object.values({key:"val"}).includes("val")
); // Returns true

Why is this simple JSON.parse JavaScript code failing? [duplicate]

This question already has answers here:
Why [] == [] is false in JavaScript?
(6 answers)
Weird behavior of comparison operator JavaScript when using empty array [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to understand why this simple code is saying false instead of true.
let test1 = JSON.parse("[]");
let test2 = JSON.parse("[]");
console.log(test1 == test2);
Surely this code should be true but I always get it saying false. Whats the issue?
Thanks

Why does a "for ... in" loop—for (var/let/const A in B)—make A a string in vanilla ES6? [duplicate]

This question already has answers here:
javascript for loop counter coming out as string [duplicate]
(3 answers)
Closed 3 years ago.
This is in Chromium 78:
for (var i in [1,3,5]) console.log(i+1)
Now, I expected for (var i in [1,3,5]) console.log(i+1) to output 1, 2, 3, because i should be an index value. I know the MDN docs mention that the order may come out strangely in this case, but why the type conversion?
i is not the index, i is the property key of the array object. Property keys are always strings.

Issue working with comparison operator [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 4 years ago.
Console logging this.byPassViewState returns ["01"]
if i do this.byPassViewState === ['01'] it returns false
typeof(this.byPassViewState) retuns object
My question is why this.byPassViewState returns false ? it suppose to be true right ? please tell me what i'm doing wrong here
You cannot compare 2 array with just using == or === operators.
The easiest way to compare array is using JSON.stringify().
let byPassViewState = ["01"];
let compare = (JSON.stringify(byPassViewState) == JSON.stringify(["01"]) );
console.log(compare);
Please Reference : How to compare arrays in JavaScript?

Finding property in Object [duplicate]

This question already has answers here:
How do I check if an object has a specific property in JavaScript?
(31 answers)
Closed 6 years ago.
Imagine this scenario:
var myObject = {
"1030":{},
"1059":{}
}
I want to check if 1030 is in that object.
How would I do this?
Try hasOwnProperty
if(myObject.hasOwnProperty("1030")) {
// Do code
}
It's a bit safer than checking if(myObject["1030"]). This will return false, if the value is falsey (false, undefined, null), which may be desirable, but also does not strictly mean it does not exist.

Categories