0

I'm really confused of getting the keys of arrays or objects.

How to get the key of:

var arr = [0, 2, 5];
findKey(array, 5);

How to get the key;

var obj = {'Abc': {name: 'Sample'}};
findKey(obj, 'Sample'); // Find the index, not abc. Get 0 here.

How to get the key:

var obj = {'Room1': ['User1', 'User2', 'User3']};
findKey(obj, 'User1'); // Find the index, not Room1. Get 0 here.
Canser Yanbakan
  • 3,780
  • 3
  • 39
  • 65
  • Unfortunately your second example isn't possible. That's a JavaScript object and keys aren't ordered in a JavaScript object so there's no reliable way to produce the result you want. And how would the function perform if you gave it `{'Room1': [1, 2, 3], 'Room2': [4, 5, 6]}`? – Justin Niessner Dec 22 '14 at 16:40
  • @haim770 I don't think so... – Canser Yanbakan Dec 22 '14 at 16:46

0 Answers0