public
Last active

Javascript sugar: get your keys or values of an object

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//lo sauer '12
var o={a: 1, b: 2, c: 3},k
//#1 method
for(k[k.length] in k=[],o); //keys
//>k:
//> ["a", "b", "c"]
#2 elegant
Object.keys(o)
//> ["a", "b", "c", "i", "length"]
Object.getOwnPropertyNames(o)
//>["c", "length", "i", "b", "a"]
 
//to get values no sugar-version exists
var v=[]
for(var i in o)v.push(o[i]);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.