-2

I have the following object:

{1235: "2.03", 1234: "3.05", 1236: "3.05"}

How do I get the index given the key?

Example: key = 1236 then index would be "2"

wwjdm
  • 2,178
  • 6
  • 27
  • 51

2 Answers2

2

Objects are also called associative arrays. Objects typically store a key value, while Arrays typically store just values so the term index does not make sense for objects while it does for for Arrays. In objects you can iterate over keys or fetch values for a particular key. You might want to rethink the data structure for storing and fetching your data as you may run into browser specific issues (particularly on older browsers or mobile browsers)

you may want to read the difference associative array versus object in javascript and where to use them at understanding objects vs arrays

Karsankaka
  • 104
  • 5
0

you can pass also the index if you iterate trough the keys of the object.

var obj = {1235: "2.03", 1234: "3.05", 1236: "3.05"};

Object.keys(obj).forEach(function (prop, index) {
  console.log(prop + '_' + index);
});
oezkany
  • 141
  • 2
  • 8