1

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?

2
  • 1
    show the line where it is defined. Commented Jul 30, 2014 at 9:50
  • var AB = { pName:"AB", p_r:70, p_r_OK:80, logoURL:"../images/AB512.png" }; Commented Jul 30, 2014 at 9:52

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is better than my answer. In a matter of fact, Douglas Crockford recommends the dot notation over the array notation.
@user3518823, expand on "no success" are you getting the same error, a different error, or is something else happening
4

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;

Comments

1

you can use

AB['p_r'] = response[0].result; OR

AB["p_r"] = response[0].result; OR
AB.p_r = response[0].result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.