Turn Objects into arrays

davidbell_xyz

David Bell

Posted on October 21, 2020

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

đź’– đź’Ş đź™… đźš©
davidbell_xyz
David Bell

Posted on October 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related