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
.