Search code examples
javascriptfor-loopidentifier

Using variable as identifier in object


I have a code like this:

for (var i = 0; i < result.Table.length; i++) {
    for (var x = 0; x < count; x++) {                           
        var xIndex = 'F' + (x + 1)
        var item = result.Table[i].xIndex;
        console.log(item)
    }
}

But unfortunately I can't use xIndex in example as this. So how to change script to be able to set xIndex as identifier?

Thanks.


Solution

  • You can't use the dot syntax there, just use brackets and it'll work

    result.Table[i][xIndex]