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

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

Related

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']

How to use variable to dynamically change object attribute? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
let property = $(this).val();
let property_name = this.dataset.property;
headObject[selected].property_name = property;
});
property_name is a variable not key. Are there anyway to handle this? Thank you in advance!
I can see you are uing jQuery.
headObject[selected].attr( property_name, property ); //Where headObject[selected] is a jQuery object

How to access to object properties by string with another object [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I need to access to object properties by string with another object
In php you can reach that like if array $b = 't';$a->$b; is the same as $a->t
var obj = {
title : 'spider'
}
var hero = {
spider : 'is a hero'
}
console.log( hero.spider );//works fine
console.log( hero.obj.title );//i need the same result, from variable (obj)
how can i do that
console.log(hero[obj.title])
I think this should work.

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

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

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

Categories

Resources