
Image by Kelvin Ang from Unsplash
DESCRIPTION
Converting data structures is a day-to-day job as a Developer - so is staying up-to-date with the language and it's features.
From object to array
x
const obj = {
a: 1,
b: 2,
c: 3
};
// All of the below are now an array,
// you can use all the array methods and properties,
// e.g. .reduce or .length
const keys = Object.keys(obj); // ['a', 'b', 'c']
const values = Object.values(obj); // [1, 2, 3]
const entries = Object.entries(obj); // [["a", 1], ["b", 2], ["c", 3]]
Summary
- The above are ES8 (ECMAScript 2017) new methods which are extremely helpful when converting from an
Object
into anArray
.