Skip to content
Announcing the new Pro Zone. Check it out!

Object.entries() Method – Copy an Object's Key-Value Pairs into an Array

Whenever you use Object.entries() on a JavaScript object, the method does the following:

  • It creates a new array.
  • It copies the specified object’s enumerable own key-value pairs into the newly created array.
  • It returns the new array containing one or more nested arrays. Each nested array has two items: the property’s key (always represented as a string item) and the property’s value.

Syntax of the Object.entries() Method

Object.entries() accepts a single argument. Here’s the syntax:

Object.entries(objectArgument);

The objectArgument refers to the JavaScript object whose key-value pairs you want to extract into a new array.

Examples of the Object.entries() Method

Below are examples of the Object.entries() method.

Copy an object’s key-value pairs into a new array

const profile = {
firstName: "Oluwatobi",
lastName: "Sofela",
companyName: "CodeSweetly",
npmPackage: "@codesweetly/react-youtube-playlist",
};
const formData = Object.entries(profile);
console.log(formData);
// The invocation above will return:
// [
// ["firstName", "Oluwatobi"],
// ["lastName", "Sofela"],
// ["companyName", "CodeSweetly"],
// ["npmPackage", "@codesweetly/react-youtube-playlist"],
// ["id", 9103],
// ]

Try Editing It

Copy an array’s key-value pairs into a new array

const myBio = ["Oluwatobi", "Sofela", "CodeSweetly", 9103];
const itemsInMyBio = Object.entries(myBio);
console.log(itemsInMyBio);
// The invocation above will return:
// [
// ["0", "Oluwatobi"],
// ["1", "Sofela"],
// ["2", "CodeSweetly"],
// ["3", 9103],
// ]

Try Editing It

Copy a string’s key-value pairs into a new array

const firstName = "Oluwatobi";
const itemsInFirstName = Object.entries(firstName);
console.log(itemsInFirstName);
// The invocation above will return:
// [
// ["0", "O"],
// ["1", "l"],
// ["2", "u"],
// ["3", "w"],
// ["4", "a"],
// ["5", "t"],
// ["6", "o"],
// ["7", "b"],
// ["8", "i"],
// ]

Try Editing It

The Object.entries() method successfully extracted the string’s key-value pairs because strings are the only primitive data with enumerable own properties.