Posted By

noah on 06/11/07


Tagged

object library trim String hash utilities keys


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
metoikos


keys: get an array of all property names in an object


 / Published in: JavaScript
 

do the same thing as the Perl "keys" subroutine

  1. //do the same thing as the Perl "keys" subroutine
  2. keys : function (o) {
  3. var accumulator = [];
  4. for (var propertyName in o) {
  5. arr.push(propertyName);
  6. }
  7. return accumulator;
  8. }
  9.  
  10. //get values instead of keys
  11. values : function (o) {
  12. var accumulator = [];
  13. for (var propertyName in o) {
  14. arr.push(o[propertyName]);
  15. }
  16. return accumulator;
  17. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: bwagner on January 16, 2011

The above code does not work. Replace arr with accumulator.

//do the same thing as the Perl "keys" subroutine keys : function (o) { var accumulator = []; for (var propertyName in o) { accumulator.push(propertyName); } return accumulator; }

//get values instead of keys values : function (o) { var accumulator = []; for (var propertyName in o) { accumulator.push(o[propertyName]); } return accumulator; }

You need to login to post a comment.