-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"

2
  • 1
    "Index" of an object doesn't make sense. Index of an array, on the other hand, does. Commented Jun 26, 2019 at 20:17
  • 2
    Your questions really are showing that you are using the wrong model for your task. Commented Jun 26, 2019 at 20:20

2 Answers 2

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

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

Comments

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);
});

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.