0

I have two objects

var objA = {name:"company1", caption : "The caption..."};

I want to create the following object using objA

var company = {company1: "The caption..."};

Something like the following didn't work.

var company = {objA.name : objA.caption};
promaxdev
  • 455
  • 1
  • 6
  • 19

2 Answers2

3
var company = {};
company[objA.name] = objA.caption;

In the object literal syntax, the property name to the left of the colon is an identifier, it's not treated as an expression. So you can access the property dynamically as above.

jspcal
  • 50,847
  • 7
  • 72
  • 76
1
var objA = {name:"company1", caption : "The caption..."};
var company = {};
company[objA.name] = objA.caption;