Categories
JavaScript Answers

How to convert an object to an array of key-value pairs in JavaScript?

Spread the love

To convert an object to an array of key-value pairs in JavaScript, we call the Object.entries method.

For instance, we write

const obj = { 1: 5, 2: 7, 3: 0, 4: 0, 5: 0, 6: 0 };
const result = Object.entries(obj);

console.log(result);

to call Object.entries with obj to return an array with array of key-value pairs in obj.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *