Object entries help you see all the keys and values inside an object as pairs. This makes it easy to work with each part separately.
Object entries in Javascript
const entriesArray = Object.entries(objectName);
This returns an array where each item is a two-element array: [key, value].
The keys are always strings, and values can be any type.
const user = { name: 'Alice', age: 25 }; const entries = Object.entries(user); console.log(entries);
const emptyObject = {}; console.log(Object.entries(emptyObject));
const singleEntry = { city: 'Paris' }; console.log(Object.entries(singleEntry));
const mixedTypes = { id: 101, active: true, data: null }; console.log(Object.entries(mixedTypes));
This program shows an object with product details. It prints the object, then prints the entries array, and finally loops through each key-value pair to print them separately.
const product = { name: 'Laptop', price: 1200, inStock: true }; console.log('Before using Object.entries:'); console.log(product); const productEntries = Object.entries(product); console.log('\nAfter using Object.entries:'); console.log(productEntries); // Loop through entries and print key and value for (const [key, value] of productEntries) { console.log(`Key: ${key}, Value: ${value}`); }
Time complexity is O(n) where n is the number of keys, because it reads all keys and values.
Space complexity is also O(n) because it creates a new array with all entries.
A common mistake is to expect Object.entries to return an object, but it returns an array of arrays.
Use Object.entries when you want to work with keys and values together, especially for loops or transformations.
Object.entries converts an object into an array of [key, value] pairs.
This helps you loop through or manipulate object data easily.
Remember it returns an array, not an object.