0

I have an object looking like this :

{ 
Item 1 : {'Item1a', 'Item1b', 'Item1c'},
Item 2 : {'Item2a', 'Item2b', 'Item2c'},
Item 3 : {'Item3a', 'Item3b', 'Item3c'}
}

I want to know the index of Item2 for instance. I tried myobject.indexOf('Item 2');

But the console tells me : indexOf() is not a function. Do you ave an idea ?

Pauloscorps
  • 564
  • 1
  • 6
  • 16
  • indexOf is a method of an Array Object but this looks like JSON. – Coldblue Sep 18 '17 at 10:29
  • https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object - I believe you will find what you need here. – Coldblue Sep 18 '17 at 10:29
  • @Coldblue: That's not JSON. It's a object. – Cerbrus Sep 18 '17 at 10:30
  • `indexOf` is a prototype of `Array`, not of `Object` Try `console.log(Object.keys(obj).indexOf('Item 2'));` – Rayon Sep 18 '17 at 10:31
  • 1
    I'm not sure, if the linked dup is an answer to this question. Anyway, objects don't have indices, or any specific order in the first place. – Teemu Sep 18 '17 at 10:32
  • 1
    @Rayon: The result to that code will be different, depending on the browser you run it on. – Cerbrus Sep 18 '17 at 10:32
  • 1
    @Cerbrus - I agree with you, I feel one should not rely on order of keys. If order matters then one should opt for array instead of `object` – Rayon Sep 18 '17 at 10:33
  • 1
    @Cerbrus in most cases one would not really care about browsers discrepancy in this case, that sounds pretty improbable that one will use the index gotten from there to use it in an other browser. The real problem is what will one do with this index anyway. One can't access its value from this index. – Kaiido Sep 18 '17 at 10:39

0 Answers0