Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lsauer
Created May 22, 2012 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsauer/2770037 to your computer and use it in GitHub Desktop.
Save lsauer/2770037 to your computer and use it in GitHub Desktop.
Javascript sugar: get your keys or values of an object
//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]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment