From Object to Array and back

Easily convert an Object to an Array and viceversa

Consider the following Object defined as "startingObject"

const startingObject = {
  key1: 'value 1',
  key2: 'value 2',
}

startingObject can be converted to an Array:

by keys and values

Object.entries(startingObject)
// [['key1', 'value 1'], ['key2', 'value 2']]

only by keys

Object.keys(startingObject)
// ['key1', 'key2']

only by values

Object.values(startingObject)
// ['value 1', 'value 2']

Consider the following Array defined as "startingArray"

const startingArray = [
  ['one', 1],
  ['two', 2],
]

startingArray can be converted to an Object:

Object.fromEntries(startingArray)
// {one: 1, two: 2}