LoginSignup
4

More than 5 years have passed since last update.

Object.keys(), Object.values()

Posted at

これだけのために UnderScore.js をincludeするのもなぁと思って自作。

Object.prototype.values = function(){var o=this;var r=[];for(var k in o) if(o.hasOwnProperty(k)){r.push(o[k])}return r};
Object.prototype.keys   = function(){var o=this;var r=[];for(var k in o) if(o.hasOwnProperty(k)){r.push(  k )}return r};

Usage

var obj = {key1: 'foo', key2: 'bar'};
obj.keys();   //=> ["key1", "key2"]
obj.values(); //=> ["foo", "bar"]

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4