Finding property in Object [duplicate] - javascript

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.

Related

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.

React Native/Javascript - How to know that object does not have the property? [duplicate]

This question already has answers here:
Checking if a key exists in a JavaScript object?
(31 answers)
Closed 3 years ago.
Let say :
myVariable.color;
myVariable.height;
myVariable.width;
But sometimes, myVariable only have "color" and "height" property.
I have try this, but no luck :
if(myVariable.width == undefined) {
//but not filtered here
}
How to know if myVariable doesn't contain "width" property by code, is it possible ?
You could try to double negate:
if(!!myVariable.width){
//do something here
}
You are looking for hasOwnProperty.
If you would like to perform a search in the whole potential prototype chain of an object, you can also use the in operator.
if (width in object) {

Why === and == giving false for following? [duplicate]

This question already has answers here:
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 4 years ago.
I know it is very much stupid to ask but can anyone tell me
Why === and == giving false for following.
x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);
Here typeof(x[0]) and typeof([1,2]) is also same, then why it is giving false?
Because they are different values in memory.
x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory
var y = x[0]; //Same value in memory
console.log(x[0]===y);
console.log(x[0]==y);
Equality comparisons and sameness

Why {} !== {} in Javascript [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.

what does 'in' mean in javascript if statement [duplicate]

This question already has answers here:
Javascript if in x [duplicate]
(3 answers)
Closed 6 years ago.
Hi i'm following a tutorial learning to use javascript.
function move(keyclick) {
if(40 in keyclick) {}
playerMove.x++;
render(); }
What does the 'in' word mean? I understand what the function is doing, but why not just use ==
?
Thanks
The in operator is true if the string on the LHS is the name of a property that exists on the object on the RHS.
== tests if a value matches another value, which is entirely different.
The in operator returns true if the specified property is in the specified object (cited from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).

Categories