This question already has answers here:
Using integer keys with dot notation to access property in javascript objects
(3 answers)
Closed 6 years ago.
Let's say I have an object:
var elObject = {
one: {
name: "Oliver"}
}
I can access name by doing elObject.one.name and everything is great, but let's say I have this instead:
var elObject = {
1: {
name: "Oliver"}
}
Suddenly, I can't access name through elObject.1.name anymore since I'm using 1 instead of 'one'. Is there a a special escape or something I'm supposed to use with object literal and digits?
You can declare the plain object with number 1 using it as string. Once is not allowed to have a property name starting with number, you can access it using bracket notation.
Example and findle below.
var x = {
'1' : {
name: 'Joao'
}
};
alert(x);
try {
alert(x['1'].name);
}
catch(e){
alert(e.message);
}
https://jsfiddle.net/b4c34wLv/
Related
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 5 months ago.
const lookup = {
alpha : 'Adams',
bravo : 'Boston',
charlie : 'Chicago',
delta : 'Denver',
echo : 'Easy',
foxtrot : 'Frank'
}
result = lookup[val];
im confused as to why i am able to do lookup[val] but lookup.val would not work here.
or something like
const testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
const playerNumber = 16;
const player = testObj[playerNumber];
testObj[playerNumber] would work but testObj.playerNumber would not. i initally assumed because 12,16,19 were int but even changing them to strings would be the same result where dot notation dont work but bracket will.
when storing an objects property into a var are we only allowed to use bracket? is so, how come if i was to do console.log(testObj.playerNumber) would not work but console.log(testObj[playerNumber]) will?
By doing testObj.playerNumber you are not invoking the variable playerNumber, but rather accessing a playerNumber property within the testObj object.
Something like this:
const testObj = {
playerNumber: "12"
};
testObj.playerNumber // 12
This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Accessing a JavaScript's object property without knowing that property name
(3 answers)
JavaScript: Get first and only property name of object
(7 answers)
How do I access properties of a javascript object if I don't know the names?
(8 answers)
Closed 3 years ago.
I have the following JavaScript object:
{
"_embedded": {
"dealerListItemDToes": [
{
...
},
{
...
}
]
}
}
Property called 'dealerListItemDToes' will always be at the given position in the object but its name can vary depending on the HTTP requests.
How can I access the property 'dealerListItemDToes' and retrieve its content without referencing its name?
Since it's the only property of the _embedded object, you could access the [0]th item in an array of object entries:
const obj = {
"_embedded": {
"dealerListItemDToes": [
{
// ...
},
{
// ...
}
]
}
};
console.log(
Object.entries(obj._embedded)[0]
);
You can try like this
let data = {
"_embedded": {
"dealerListItemDToes": [{
"h": 1
}]
}
}
console.log(data._embedded[Object.keys(data._embedded)[0]])
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I am working on the following snippet. Why am I not able to get the
let node = 'Em2';
console.log(data.node.c2);
work? As you can see I am able to get data while passing data.Em2.c1 but a dynamic format like this let node = 'Em2'; console.log(data.node.c2); I am getting this error
TypeError: Cannot read property 'c2' of undefined
Code:
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
Use square braces [] to access object member via variable
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data[node].c2);
Similar question: how to access object property using variable
Instead of console.log(data.node.c2), try console.log(data[node][c2])
This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Using Variable for Property Name of Object - Javascript [duplicate]
(2 answers)
Closed 4 years ago.
I have an array that contains variables that are used to control a template.
divisionsListToHide: ['showActivitiesList',
'showAssignActionplanDiv',
'showPropertiesListDiv',
'showAddActivityDiv',
'showCurrentActivity',
'editCurrentActivity'
],
I want to call a method that will set all variables contained in the divisionsListToHide array to false, i.e.
this.showAssignActionplanDiv = false;
this.showPropertiesListDiv= false;
I am trying to create a method (function) to accomplish this:
hideDivisions: function()
{
for(var i = 0;i<this.divisionsListToHide.length;i++)
{
var xx = 'this.' + this.divisionsListToHide[i]; // e.g. this.showActivitiesList
console.log(xx);
eval(xx) = false;
}
},
I was hoping to accomplish this through eval(), however, it displays "Uncaught ReferenceError: Invalid left-hand side in assignment". Any suggestion, how to accomplish this?
this is an object, and you can access its properties in two ways:
this.propertyName and this["propertyName"]. These two ways are the same, except the second one can be used with a variable just like in your case:
var prop = "propertyName";
this[prop] = false; // is equal to this["propertyName] = false, which is equal to this.propertyName = false
So try this:
hideDivisions: function()
{
for (var i = 0; I <this.divisionsListToHide.length; i++)
{
this[this.divisionsListToHide[i]] = false;
}
}
This question already has answers here:
Assigning the value of another key in javascript [duplicate]
(2 answers)
Closed 7 years ago.
According to me this is only being used if you need to call constructor like below
var dogObj = function(){
this.bark = "woof";
}
var dog1 = new dogObj();
console.log(dog1.bark);
Is there any cases where I use this on object literal?
var bark = {
this.bark = "dog"
}
Is above code valid? If yes,how do I call it?
you can use
var bark = {
bark : "dog"
}
and call it like
console.log(bark.bark)
this.bark = "woof";
Is not valid.
You have try something like below
var dog = {
bark : "dog"
}
And then you can access like console.log(dog.bark)
Once you formed any json string you can validate the sting using http://jsonlint.com/ just place your json string and the click on validate.