Javascript - Array with Boolean Keys?
You can't use arbitrary indexes in an array, but you can use an object literal to (sort of) accomplish what you're after:
var test = {};
test[false] = "asdf";
test['false'] = "fdsa";
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'
.
test[false] === test['false'] === test.false
This is why your first example's Object.keys().length
call returns just 1
.
For an excellent getting started guide on objects in JavaScript, I would recommend MDN's Working with objects.
Arrays in Javascript aren't associative, so you cannot assign values to keys in them.
var test = [];
test.push(true); // [true]
test.push(false); // [true, false]
You're interested in an Object!
var test = {};
test[true] = "Success!";
test[false] = "Sadness"; // {'false': "Sadness", 'true': "Success"}