I want to use an array to look up a value given a key. AFAIK, it should be possible when converting the float to a string, like the second example below: (jsfiddle)
arr = [];
arr[1.3] = "One point three";
console.log(arr.length);
arr = [];
arr["1.3"] = "One point three";
console.log(arr.length);
But both result in a zero-length array. What am I doing wrong? I.e. how can I look up an object/string/whatever given a float value?
It would be awesome to have a reference guide on common operations when using having to look up values, such as:
- get an element, given a float key
- get total number of elements
- test if float key exists
- put new float key / value pair
- and maybe others, such as loop through all keys/values
arr = {}; arr['1.3'] = "One point three"; console.log(Object.keys(arr).length);lengthdoesn't include non-integer keys, otherwise it would have to count itself.