Convert JavaScript data structures
From object to array
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
- Object.keys(), Object.values(), Object.entries().
- The above are ES8 (ECMAScript 2017) new methods which are extremely helpful when converting from an
Objectinto anArray.
Try this in our interactive code editor
Practice hands-on with our built-in code sandbox.
Open Code Editor
Creator of BigDevSoon
Full-stack developer and educator passionate about helping developers build real-world skills through hands-on projects. Creator of BigDevSoon, a vibe coding platform with 21 projects, 100 coding challenges, 40+ practice problems, and Merlin AI.
Related Pills
JavaScript multiple condition checks
Checking multiple values in JavaScript often leads to redundant, boilerplate code - let's use .includes method to fix that.
ECMAScript new features
Null coalescing operator, optional chaining, and Array.flat method are all amazing.
CSS @starting-style
Animate elements into view from display none using @starting-style — no JavaScript timing hacks, no setTimeout workarounds.