Using a string to call an object [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 3 years ago.
I am trying to create a program that defines the variable finalResult based on variable input. The input variable should call on an object inside of object A:
var input = "";
var A = {
AA: {
result: 0
},
AB: {
result: 1
}
}
var finalResult = A.input.result;
So if input = "AA", then the final result should be 0, but if input = "AB", then the final result should be 1.

You can do A[input].result, which assumes the value of input is present as a property in A. If the property isn’t present you’ll get an error trying to access result on undefined. You can guard against this by OR’ing it with an empty object:
(A[input] || {}).result // undefined if A[input] isn’t present

Related

Spread operator with bracket notation [duplicate]

This question already has answers here:
Object destructuring with property names that are not valid variable names
(2 answers)
Closed 3 years ago.
The following works fine:
const o = {one:1,two:2,three:3};
const {one,...others}=o;//one=1, others={two:2,three:3}
But how would I do the following:
var o = {['this-is-one']:1,two:2,three:3};
var {['this-is-one'],...others}=o;
Currently that gives me a SyntaxError: Unexpected token ','
I suspect it would not work because this-is-one would be invalid for a constant name (it only works for property values).
You need a renaming of the variable name, because the the given key is not a valid variable name.
var o = { 'this-is-one': 1, two: 2, three: 3 },
{ 'this-is-one': thisIsOne, ...others } = o;
console.log(thisIsOne);
console.log(others);

Why does a const constant assign an object, and a constant can modify the value of the object? [duplicate]

This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
Closed 4 years ago.
enter image description here
Why myVariable can be modified?
const obj = {
a: 'a'
}
const myVariable = obj;
try{
myVariable = { a: 'c' } //The const declaration creates a read-only reference to a value
}catch(e){
console.log(e);
}
myVariable.a = 'b';
console.log(myVariable); //{a: "b"}
This happens because your constant is actually storing a reference to the object. When you're adding to object you're not re-assigning or re-declaring the constant,you're just adding to the "list" that the constant points to.Refer more here : Why can I change value of a constant in javascript

Undefined when referencing an object via another object in JavaScript [duplicate]

This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);

How can I get an object's property value dynamically in JavaScript? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have the following object array in which I'm storing bus route by number and then by name:
var routearray =[
ruta01 =
{
via01: "Progress",
via02: "Ten",
via03: "S",
via04: "Maria"
},
ruta02 =
{
via01: "Exterior",
via02: "Interior"
},
ruta03 =
{
via01: "University",
via02: "Henry St"
},];
And I have the following code with which I want to access the value of each property:
for(i=0;i<routearray.length;i++) //iterates through the array's objects
{
var props = Object.keys(routearray[i]); //array that stores the object properties
for(j=0;j<props.length;j++) //iterates through specific object properties
{
console.log(props[j]); //shows the property names
var propertystring = String(props[j]); //transforms the property name into string
console.log(routearray[i].propertystring]; //should access the property value
}
}
When the last line of the code executes, I get 8 "undefined" results in the console. If I change it to something like:
console.log(routearray[i].via01];
It works just fine, but I'm not sure why it is not accessing the value if the string is supposed to work correctly.
What am I doing wrong? Is there a better way?
It should be:
console.log(routearray[i][propertystring]);
In general, when you do "someObject.key" then the actual value "key" must exist as a property in "someObject". But, if you do "someObject[key]", then the value contained inside the variable "key" must exist as a property in "someObject".

How to retrieve values from a javascript object using a variable's contents as the property? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have this JSON, I am trying to retrieve values by substituting variables as below.
var model = {
regression : {
regUtils : {
subIDs : {
B00099999823 : {
"a" : "b",
"c" : "d"
}
}
}
};
var vals = "B00099999823";
alert(model.regression.regUtils.subIDs.vals.a); // error returned : Object doesn't support this property or method
alert(model.regression.regUtils.subIDs.B00099999823.a); //alerts b
How can I substitute a variable in place of a key or object.
thanks.
You can access with a variable contents like this:
var vals = "B00099999823";
alert(model.regression.regUtils.subIDs[vals].a;
You can use [] to access an object by using a variables contents. It would be the same as this:
model.regression.regUtils.subIDs['B00099999823'].a

Categories