DEV Community

David Bell
David Bell

Posted on • Updated on • Originally published at davidbell.space

Turn Objects into arrays

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

Top comments (0)