Keys In Javascript Objects Can Only Be StringsMarch 24, 2014

Happened to me lately.

The backstory is that the actual property names i was using were passed to me by browser (touch event identifiers) as numbers in touchstart event. Here’s the simplified to barebones code:

var i = 1
var O = {}
O[i] = 'foo'
var K = Object.keys(O)
K.indexOf(i) // -1

And the result is naturally… -1,
as property name can only be string and Object.keys() returns array of strings. The same property as number was passed by touchend event and my search by that failed miserably.
Took me some time to find the cause.