Convert Object to Array with Javascript

javascript
Published on March 24, 2021

A Javascript object can be converted to an array using Object.keys(), Object.values() or Object.entries().

  • Object.keys() method returns an array of the object's property names.
  • Object.values() method returns an array of the object's property values.
  • Object.entries() method returns an array of the object's property names and values.

For the all three methods, the order of the items in the created array is the same as that when the object is looped manually (using for..in).

Object Property Names to Array Using Object.keys()

The Object.keys() method returns an array of the object's property names.

let ob = {
	id: 100,
	name: "John",
	age: 28,
};

let arr = Object.keys(ob);

// ["id", "name", "age"]
console.log(arr);

Object Property Values to Array Using Object.values()

The Object.values() method returns an array of the object's property values.

let ob = {
	id: 100,
	name: "John",
	age: 28,
};

let arr = Object.values(ob);

// [100, "John", 28]
console.log(arr);

Object Property Names & Values to Array Using Object.entries()

The Object.entries() method returns an array of the object's property names & values. Each element in the created array is itself an array of [name, value].

let ob = {
	id: 100,
	name: "John",
	age: 28,
};

let arr = Object.entries(ob);

// [["id", 100], ["name", "John"], ["age", 28]]
console.log(arr);
In this Tutorial
    Comments
    Loading Comments