1

Is there any option as I can use a variable on the left side of my object declaration? Something like this:

var col = 'col';
var gridDataVK4000 = {
    items : []
};

for (var i = 0; i <= 14; i++) {
    col += col + i;
    // we now push to the item property
    gridDataVK4000.items.push({
        col : i,
    });
}

because my example isn't working. :(

Adrien Brunelat
  • 4,134
  • 4
  • 29
  • 42
h0ppel
  • 339
  • 1
  • 4
  • 20

1 Answers1

2

You'll need to declare it outside of the curly braces using square bracket notation, otherwise you're assigning the key "col", literally, to your object.

var result = {};
result[col] = i;

gridDataVK4000.items.push(result);
James Donnelly
  • 122,518
  • 33
  • 200
  • 204