Object.entries and Object.fromEntries in JavaScript

Object.entries

It takes an object and returns an array of that object’s own enumerable string-keyed property [key, value] pairs.

const person = {
    name: 'Bruce',
    age: 30,
    job: 'Engineer'
};
const entries = Object.entries(person);
console.log(entries);
// Output: [['name', 'Bruce'], ['age', 30], ['job', 'Engineer']]

Object.fromEntries

Does the reverse of Object.entries. It takes an array of key-value pairs and converts it back into an object.

const entries = [['name', 'Bruce'], ['age', 30], ['job', 'Engineer']];
const person = Object.fromEntries(entries);
console.log(person);
// Output: { name: 'Bruce', age: 30, job: 'Engineer' }

Leave a comment

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