How do I get values from an object using dot notation [duplicate] - javascript

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed last year.
How do I get values from an object using dot notation when I have a variable that changes.
console.log(`${getData}.BUTTON_${item.UUID}`);
// outputs: [object Object].BUTTON_1
In the object is a value for BUTTON_1. My item.UUID is a number that will change to a different button number. I need to be able to get the value for the button number
what works
console.log(getData.BUTTON_0);
My button number will be between 0 and 4 example getData.BUTTON_0, getData.BUTTON_1 ...
currently I need the button number to be a variable however the following does not work
console.log(`${getData}.BUTTON_${item.UUID}`);

JavaScript object: access variable property by name as string
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#bracket_notation
console.log(getData[`BUTTON_${item.UUID}`]);

Related

the way of accessing the number element of object in javascript in '.' causes an error [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
How can I access object properties containing special characters?
(2 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 1 year ago.
First, I am a beginner of javascript.
Please understand asking this simple question.. (looks simple)
I was making a membership related webpage and encountered an error..
I was learned that to access the element of an object, I can use '.'(dot)
so I coded like,
It was a bit long object(json type contents) but.. to sum up,
dict1 = {'a':1, 'b':2, 'c':3};
dict2 = {1:'a', 2:'b', 3:'c'}
dict1.a
result: 1
dict2.1
result: Uncaught SyntaxError: Unexpected number
so the point is,
Someone may think, "if you can see the elements, why would you access the element by '.'(dot). if you already know it can cause the error."
but the data is user input and users can input any data as they wish.
Javascript provides '.'(dot) operator for Object but not working for number Element??
or do I use the dot in wrong way?
If you use dot notation, your key must be a valid identifier (start with a letter, $ or _). So in this case you would need to use dict2['1'].
You can do something like this to get the values
// With the braces you can get the values even if the key is a number
let dict2 = {
1: 'a',
2: 'b',
3: 'c'
}
console.log(dict2[1])
// Even if you want to access the values that are having keys like strings you can use the same operator
dict1 = {'a':1, 'b':2, 'c':3};
console.log(dict1['a'])

How to update an attribute in object based on a variable in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript set object key by variable
(8 answers)
Closed 2 years ago.
I want to update an attribute of an object but the attribute is not known before hand and is rather stored in a variable.
I have an object "obj" like below
var obj={ a : 1, b : 2 }
if I want to change attribute "a" to 3 then I can just do
obj.a = 3
but I have a variable "attribute_to_change" which has the attribute that I want to change. I want to be able to somehow use the variable value to change the respective object attribute.
Something like below
obj.attribute_to_change = 3
Currently I have to write a separate line code of for each type of attribute that I want to change. But I can actually just make one function and pass the value and the attribute to be changed if I figure this out.

Accessing variable value by string name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I access local scope dynamically in javascript?
(4 answers)
Closed 3 years ago.
Issue: I want to access some variables I got in my js-file by a string.
I know there is an option to receive variable value if you have an object and pass the object key name in square brackets to the object, but this is not the case here. Here I have just one comon variable, and I want its value by "sending" a string down.
Example on what I want to achieve:
const constantName = 12345; // Value to access
const stringToUseToGetTheValueOfConstantName = 'constantName';
// I want to know how to get the '12345' by something like this:
const valueFinallyAccessed = `${stringToUseToGetTheValueOfConstantName}`; // I know this returns a string
console.log(valueFinallyAccessed); // 12345
Solution
const valueFinallyAccessed = eval(stringToUseToGetTheValueOfConstantName);
console.log(valueFinallyAccessed); // Prints out the value 12345

Cannot read properties of a JavsScript function [duplicate]

This question already has answers here:
Javascript Object - Key beginning with number, allowed?
(4 answers)
Closed 6 years ago.
I have a php array that I'm json_encodeing into a JavaScript object. When I preview the object in console, it looks something like this:
Object { 1="some text", 2="something else", 3="extra text"}
Shouldn't I be able to read the value for index 1 like this (pretending my object name is obj)?
obj.1
Doing that gives me the undefined error message. How would I access the value for the exact index of 1?
You can only access a property using dot notation if the property name is a valid identifier. Identifiers cannot start with a number.
You have to use square bracket notation for other properties.
obj[1]
1- Do not use the word Object
2- Use colon instead of equal
3- Do not forget the semi-colon
4- if your key is number, use braces.
var obj = { 1:"some text", 2:"something else", 3:"extra text"};
console.log(obj[1]);

Getting object properties that start with a number [duplicate]

This question already has answers here:
Can I get a javascript object property name that starts with a number?
(2 answers)
Closed 8 years ago.
I've got an object where one of the properties starts with a number. I've read that this was allowed, but I'm getting this error:
SyntaxError: identifier starts immediately after numeric literal
var stats = {"LOAD_AVG":{"1_MIN":0.08,"5_MIN":0.08,"15_MIN":0.08},"COUNTS":{"TOTAL":888,"RUNNING":1,"SLEEPING":887,"STOPPED":0,"ZOMBIE":0},"CPU":{"USER_CPU_TIME":8.9,"SYSTEM_CPU_TIME":2.4,"NICE_CPU_TIME":0,"IO_WAIT_TIME":1,"HARD_TIME":0,"SOFT_TIME":0.1,"STEAL_TIME":0},"MEMORY":{"PHYSICAL":{"TOTAL":"3921.98mb","IN_USE":"3682.652mb (93.9%)","AVAILABLE":"239.328mb (6.1%)","BUFFERS":"266.492mb (6.8%)"},"SWAP":{"TOTAL":"4194.296mb","IN_USE":"64.264mb (1.5%)","AVAILABLE":"4130.032mb (98.5%)","CACHE":"1191.328mb (28.4%)"}}};
//works fine
window.alert(stats.COUNTS.TOTAL);
//doesn't work
window.alert(stats.LOAD_AVG.1_MIN);
Here's a fiddle.
How can I access the properties that begin with a number without rewriting the PHP that generated it?
You can use bracket access for properties that aren't valid JavaScript identifiers, that goes for property names with spaces or other language symbols like +, *
window.alert(stats.LOAD_AVG["1_MIN"]);
You can use bracket access anywhere really
window.alert(stats["COUNTS"]["TOTAL"]);

Categories

Resources