How to Reference an Object's Property by a String? [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I have seen other questions asked on Stack Overflow about doing this, but I felt that they weren't answering the right question. They always answered how to access the name of an object with a string. However, I want to know a way to do this:
var obj = {
property: "hello!"
}
console.log(obj."property");//want it to log hello!

You just access it like it's an array index, so:
console.log(obj["property"]);

Related

How to concatenate variable names in objects [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I am checking a value from an input text a form, the name of the input is
name="name['title']"
title, doing something like this:
if (!values.title) {
errors.title = 'Required';
}
I have to do this for some other controls, so I thought I could make the control name dynamic, how can I achieve something like this?
let name = 'title';
if (!values.<name>) {
errors.<name> = 'Required';
}
I updated the question to show how it is different from proposed solutions.

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

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

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";

How do I get the Value of a specified Key in JavaScript? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I know this question has been asked a million times, I just can't seem to make the answers I find fix my problem.
All I'm trying to do is return the Value of the key that is specified. They Key is in the variable "t":
RefDataTables.forEach(function(t, ti) {
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].t);
});
I know the ".t" is wrong because that is looking for a property (I'm not sure the right word to call it) named "t"
Since "t = "HandleType"" What I'm trying to accomplish is the equivalent of:
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].HandleType)
You can access it by passing the variable between the square brackets.
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0][t])

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