Posted By

garside on 12/12/08


Tagged

object jquery dictionary keys manipulation


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jeremyhcobb
umang_nine
erlhal


jQuery Object Keys


 / Published in: jQuery
 

URL: http://groups.google.com/group/jquery-en/browse_thread/thread/3d35ff16671f87a2\

Simple snippet to return the keys of an object.

  1. $.extend({
  2. keys: function(obj){
  3. var a = [];
  4. $.each(obj, function(k){ a.push(k) });
  5. return a;
  6. }
  7. })
  8.  
  9. // Useage:
  10. var obj = {a: 1, b: 2, c: 3, d: 4, kitty: 'cat'}
  11. alert($.keys(obj)); // a,b,c,d,kitty

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: martijntje on May 10, 2011

This is unnecessarily expensive in browsers that have native support for Object.keys. Therefor it is better to check for the existence of this function and use it when available:

$.extend({ keys: function(obj){ if (typeof Object.keys == 'function') return Object.keys(obj);

    var a = [];
    $.each(obj, function(k){ a.push(k) });
    return a;
}

});

You need to login to post a comment.