Turn Objects into arrays
David Bell
Posted on October 21, 2020
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' ]
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 ]
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 ] ] */
Let's connect
đź’– đź’Ş đź™… đźš©
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
javascript Unlocking Better JavaScript Performance: Avoid These Common Pitfalls 🚀
November 24, 2024
javascript Goodbye Exceptions! Mastering Error Handling in JavaScript with the Result Pattern
November 22, 2024