Skip to content

Instantly share code, notes, and snippets.

@andrit
Last active July 27, 2018 16:28
Show Gist options
  • Save andrit/13dac89bbdd5c281e13b37b24c33b373 to your computer and use it in GitHub Desktop.
Save andrit/13dac89bbdd5c281e13b37b24c33b373 to your computer and use it in GitHub Desktop.
convert object to an array in js
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map(function(key) {
  return [Number(key), obj[key]];
});

console.log(result);

or

var obj = {
  foo: 29,
  bar: 42
};
var arr = Array.from(Object.keys(obj), k=>obj[k]);

or

const obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};

console.log(Object.entries(obj).map(e => Object.assign(e, { 0: +e[0] })));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment