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,492
  • 4
  • 29
  • 42
h0ppel
  • 347
  • 2
  • 5
  • 21

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
  • 126,410
  • 34
  • 208
  • 218