I’m relatively new to Javascript, and there’s probably just a trick I’m not familiar with, but how can I assign boolean values to Array keys?
What’s happening:
var test = new Array();
test[false] = "asdf";
test['false'] = "fdsa";
Object.keys(test); // Yield [ "false" ]
Object.keys(test).length; // Yield 1
What I want to happen:
var test = new Array();
//Some stuff
Object.keys(test); // Yield [ "false" , false ]
Object.keys(test).length; // Yield 2
You can’t use arbitrary indexes in an array, but you can use an object literal to (sort of) accomplish what you’re after:
However it should be noted that object properties must be strings (or types that can be converted to strings). Using a boolean primitive will just end up in creating an object property named
'false'.This is why your first example’s
Object.keys().lengthcall returns just1.For an excellent getting started guide on objects in JavaScript, I would recommend MDN’s Working with objects.