-1

I have this code, but I want the belongsto var to be retrieved as the key using the var value and not the var name:

var belongsto = panel.attr('data-belongsto');
var panelid = panel.attr('id');

tabValue.push({belongsto:panelid}); console.log(tabValue);

this returns [{'belongsto':'12345'}]

As you can see the the script takes belongsto as the key name but I need it to take the content of the variable.

Any help is appreciated, thanks-

6
  • Look for "bracket notation". Commented Jun 29, 2014 at 18:01
  • 5
    var d = {}; d[belongsto] = panelid; tabValue.push(d); Commented Jun 29, 2014 at 18:01
  • There are no associative arrays ? Commented Jun 29, 2014 at 18:04
  • stackoverflow.com/questions/11508463/… Commented Jun 29, 2014 at 18:05
  • 1
    Also a duplicate: stackoverflow.com/q/2274242/218196. However, in ES6 it will be possible to use any expression as property name. It will look something like {[belongsto]: panelid}. (See people.mozilla.org/~jorendorff/… for more "info") Commented Jun 29, 2014 at 18:10

1 Answer 1

0

Create the object first

var obj = {};
obj[belongsto] = panelid;
tabValue.push(obj);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.