This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I need to know why this isn't working. My Javascript code accesses a variable in an object. But it appears not to be working, partly because I can't figure out the syntax.
var obj = {
size:"small",
big:false,
thing:true
}
alert(obj[size]);
I'm just not sure if I got the syntax right…
This will work here.
obj.size //returns small
OR
obj["size"] //returns small
OR
var my_var = "size"
obj[my_var] //returns small
You can reference object values either by:
obj["size"]
or
obj.size
However, there is an exception. For instance, if you have following object with a number key: (Note: key is still a string even if it's defined this way):
var obj = {
1: true
};
You can retrieve it's value only by using: obj["1"]
Hence, using obj.1 will cause a syntax error.
Therefore, your code works if you change it to e.g.: alert(obj["size"]); but I prefer to use console.log(obj["size"]); for debugging. At least, if you are playing with node.js as your tags indicates.
Cheers.
Related
This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Does javascript have a method to replace part of a string without creating a new string?
(4 answers)
Closed 1 year ago.
var a = 'cat' ;
a[0] = 'r' ;
a = 'cat'
Why..??
In case of string although you can access elements by array notation, if you try to change its content it will fail silently i.e. will not throw any error but will not change content either.
Please explain me detail.
Strings are primitive values in javascript, and are therefore immutable. This is just how the language works so there's not much to explain besides that. You can read more about it here!
It is not throwing an error because you're probably not running it in strict mode.
Strings are immutable, in JavaScript only objects and arrays are mutable. You can search for mutable data types in JavaScript in Google.
"A mutable object is an object whose state can be modified after it is created." MDN.
You can read more here: Mutable
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am unable to get the data of an object
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a.val[0])
}
fun('ab','ef')
It should output 'cd' but it is giving out error in the console
any idea how do i fix this...
Use bracket notation like so:
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a[val[0]])
}
fun('ab','ef')
Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).
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.
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 6 years ago.
how would I make an object in javascript with a computed name.
Context:
I am making an add-on that will sit in the browser, log each hostname visited, create an object named after each one. I'm replacing "." with "_"
So for example on this site it would create a stackoverflow_com object.
is this possible?
another example would be
var 1+1 (with the variable actually being 2)
I know brackets make this possible with properies but I don't know how to do it with the name itself.
You need to store them either in a global object (like window) or preferably an object of your choosing
var mySites = {};
mySites["stackoverflow_com"] = "foo"; // access as mySites.stackoverflow_com or mySites["stackoverflow_com"]
mySites[1+1] = "bar"; // access as mySites[2];
More info: JavaScript property access: dot notation vs. brackets?
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
Ok. The code here is in fact Javascript but I can not find a way to fixing the problem in javascript. The code below is javascript.
var =inputdata;
dataout = data.items.inputdata.time
This is how it would sort of look like if it was php
dataout = data.items.$inputdata.time
I would like inputdata to be treated as a variable and not as text.
Sorry for the small amount detail
You can use square bracket notation:
dataout = data.items[inputdata].time
This will allow you to use a string in place of a key for a javascript object.