How to access object property as a string in javascript [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Access JSON or JS property using string
(4 answers)
Closed 6 years ago.
I can do var foobar = foo.barto access the bar variable.
How would I do this if I had an arbitrary string that told me what element I needed to access. foo."bar" doesn't work.

I think
foo["bar"]
is the syntax you need

Related

JavaScript object like String literal doesn't to set property [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined

how to access property with hyphen in name in JS JSON [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 2 years ago.
Im trying to access an element in JSON With Hyphen in the property name
id("main").textContent = jsonData.res.main-result;
but that is causing an error
const json = {
'property-name': 'something'
}
console.log(json['property-name']);
Use jsonData.res['main-result']

Javascript - Set array member using variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
I need to be able to set the member of an array using a variable, catch being that the variable defines the name of another arrays member. I'm writing in Typescript and ES6.
The code is passed an array as a variable e but the name of the text: key will be e.[variable].
What is the correct format for setting that variable member?
options.push(
{ key: e.Id, text: e.[variable] }
);

Sentence in javascript but variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
i'm working with titanium and alloy.
I need run a JavaScript sentence like
$.D7F15.visible="true";
but D7F15 must be a variable, like
var mifranja="D7F15";
How i can run this??
some help please??
You can use bracket notation to access object property
var mifranja="D7F15";
$[mifranja].visible="true";

undefined when trying to access an element in json object [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
How come the first alert shows Dublin but the second is undefined?
http://jsfiddle.net/pzdx6L5a/1/
function getRenderParamValue(query){
customContext = JSON.parse("{\"test\":true,\"geo\":\"\",\"city\":\"Dublin\",\"categoryId\":\"\",\"categoryName\":\"\",\"productId\":\"\"}");
alert(customContext.city);
return customContext.query;
}
alert('city: ' + getRenderParamValue('city'));
It's looking for a property 'query', which doesn't exist.
Use [] instead of .:
return customContext[query];

Categories

Resources