2

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
2
  • 2
    index should be integer for an array..... use object instead and set property arr = {}; arr['1.3'] = "One point three"; console.log(Object.keys(arr).length); Commented Aug 6, 2016 at 7:46
  • length doesn't include non-integer keys, otherwise it would have to count itself. Commented Aug 6, 2016 at 7:48

4 Answers 4

1

Array's index must be an integer for adding it as an array item. If you will pass to the [] not an integer, it will be added as a property.

arr = [];
arr[1.3] = "One point three";

console.log(arr.length);

console.log(arr.hasOwnProperty('1.3'));

console.log(Object.keys(arr).length);

Sign up to request clarification or add additional context in comments.

1 Comment

Array entries are also properties.
1

I could be wrong, but it seems you are trying to create a dict. I would just use an object like others have suggested.

var arr = {
    1.2: 'One Point Three'
}

arr[1.2] or arr["1.2"] // One Point Three

or you can use es6 Map

var arr = new Map()
    arr.set(1.3, "One Point Three")
    arr.get(1.3) // One Point Three 
    arr.size // 1 

Comments

1

An array takes an integer value as index.

You should be using an Object for this kind of mapping.

var arr = {};
arr[1.3] = "One point three";
console.log(Object.keys(arr).length);

Comments

0

Suren's answer was the first, and gave me what I needed to establish a reference of operations:



Insert an item:

var x = 1.3;

// Not actually an array! arr.length will be undefined. Although
// an array could still be used, it seems
var arr = {}; 

arr[x] = "One point three";

Count items:

Object.keys(arr).length

Test if key exists:

arr.hasOwnProperty(x)

Get an element:

arr[x]

Loop through key/value pairs

for (key in arr)
{
    var value = arr[key];
}


Interesting quirks

+1 to James Emanon for several points: Firstly, showing that a string is not necessary for lookup:

arr[1.3] // works

Secondly, the array can be defined as an object like so:

arr = {
    1.3: "One point three"
};

Thirdly, ES6 provides a Map object. Will be useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.