Reputation: 25
API is returning me a value I need to assign to a certain property in my object.
However, when I try to assign this valuee to my object, I get an error:
Error Getting Data
Arguments[1]
0: ReferenceError
message: "p_r is not defined"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
callee: function (err){
length: 1
__proto__: Object
The code where I try to assign this variable looks like this:
var AB = { pName:"AB", p_r:70, p_r_OK:80, logoURL:"../images/AB512.png" };
AB[p_r] = response[0].result;
AB object has been declared before together with p_r property. Where am I making a mistake?
Upvotes: 1
Views: 1081
Reputation: 575
you can use
AB['p_r'] = response[0].result;
OR
AB["p_r"] = response[0].result; OR
AB.p_r = response[0].result;
Upvotes: 1
Reputation: 42736
you need to use quotes with the array notation:
AB["p_r"] = response[0].result;
or use the dot notation
AB.p_r = response[0].result;
Otherwise the parser will think you are trying to use a variable named p_r
Upvotes: 5
Reputation: 430
Is p_r a variable or is it the name of the property?
If p_r is the name of the property, you should do the assignment with '', like:
AB['p_r'] = response[0].result;
Upvotes: 4