DEV Community

loading...

Turn Objects into arrays

davidbell_space profile image David Bell Originally published at davidbell.space Updated on ・1 min read

Make an array of an objects Keys.

Object.keys - Creates an array of keys from an object passed in.

const fruit = {
  apples: 5,
  oranges: 10,
  lemon: 7,
  grapes: 3,
}
const fruitKeys = Object.keys(fruit)
// [ 'apples', 'oranges', 'lemon', 'grapes' ]
Enter fullscreen mode Exit fullscreen mode

Make an array of an objects values

Object.values - Creates an array from the values from an object passed in.

const fruit = {
  apples: 5,
  oranges: 10,
  lemon: 7,
  grapes: 3,
}
const fruitValues = Object.values(fruit)
// [ 5, 10, 7, 3 ]
Enter fullscreen mode Exit fullscreen mode

Make an array from an array keys and values

Object.entries - Creates an array from the keys and values from an object passed in.

const fruit = {
  apples: 5,
  oranges: 10,
  lemon: 7,
  grapes: 3,
}
const fruitEntries = Object.entries(fruit)
/* [ [ 'apples', 5 ],
  [ 'oranges', 10 ],
  [ 'lemon', 7 ],
  [ 'grapes', 3 ] ] */
Enter fullscreen mode Exit fullscreen mode

Let's connect

Twitter

Discussion

pic
Editor guide