-3

Im trying to access a property of an object using using a variable which could change

var i = '16';

var object = {
        "15" : [ 3, 4, 5, 6 ],
        "16" : [ 1, 3 ],
        "17" : [ 1, 3, 4, 5, 6 ],
        "18" : [ 1, 3, 4 ]
}
console.log( object['i'] )

using the variable gives undefined however using

console.log( object['16'] ) 

gives you the array of that propery.

Does anyone know how i could work around this

Adam
  • 2,418
  • 3
  • 17
  • 22

1 Answers1

2

Just remove quotes around your variable

var i = '16';

var object = {
        "15" : [ 3, 4, 5, 6 ],
        "16" : [ 1, 3 ],
        "17" : [ 1, 3, 4, 5, 6 ],
        "18" : [ 1, 3, 4 ]
}
console.log( object[i] )
Alex83690
  • 758
  • 9
  • 30