8

I am attempting to add a variable key, with no luck.

Here's what I got so far:

mysql('translations',{
    check: 'element_id',
    element_id: element_id,
    {'lang-'+lang_id}: value
});

The variable key is the last line of the function.

Any ideas?

Ilya Karnaukhov
  • 3,838
  • 7
  • 31
  • 53

2 Answers2

19

You can't use expressions for the identifiers in an object literal.

First create the object, then you can use dynamic names:

var obj = {
  check: 'element_id',
  element_id: element_id,
}

obj['lang-'+lang_id] = value;

mysql('translations', obj);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

You can do this:

var x = {
    check: 'element_id',
    element_id: element_id    
};
x['lang-'+lang_id] = value;

mysql('translations', x);
techfoobar
  • 65,616
  • 14
  • 114
  • 135