0
var aaa = new Object;
var bbb = 2;
var ccc = 5;
aaa[bbb].description = differentObject.ccc.description;

console.log(aaa.bbb.description);  //will report "undefined"

what I need, is to create a property "description" for each new value of bbb and assign a value to it inside the object aaa. (in my code, this, except the declaration of aaa) runs in a loop and bbb, ccc values change with each cycle).

1 Answer 1

1

a.b is equivalent to a["b"] so when you're doing aaa.bbb you're not doing anything related to the bbb variable.

aaa[bbb].description this is correct to set a property on aaa[bbb], however, aaa[bbb] needs to be an object.

An easier way do to this would be:

var aaa = {};
var bbb = 2;
var ccc = 5;
aaa[bbb] = {description: differentObject[ccc].description};
Sign up to request clarification or add additional context in comments.

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.