code_junkie

What is the most efficient way to get the first item from an associative array in JavaScript?

Nov 11th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. getKey = function (data) {
  2. var firstKey;
  3. $.each(data, function (key, val) {
  4. firstKey = key;
  5. return false;
  6. });
  7. return firstKey;
  8. };
  9.  
  10. function getKey(data) {
  11. for (var prop in data)
  12. return prop;
  13. }
  14.  
  15. function getKey(data) {
  16. for (var prop in data)
  17. if (data.propertyIsEnumerable(prop))
  18. return prop;
  19. }
Add Comment
Please, Sign In to add comment