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.
Practice
Object.entries(obj) return when used on an object obj?Solution
Step 1: Understand Object.entries behavior
Object.entries takes an object and returns an array where each element is a two-item array: the key and its corresponding value.Step 2: Compare options to definition
Only An array of [key, value] pairs from the object correctly describes this output as an array of [key, value] pairs. Options A, C, and D describe different outputs.Final Answer:
An array of [key, value] pairs from the object -> Option DQuick Check:
Object.entries = array of [key, value] pairs [OK]
- Thinking it returns an object
- Confusing entries with keys or values only
- Expecting a flat array instead of pairs
data?Solution
Step 1: Recall the correct method name and usage
The correct method isObject.entries()called with the object as argument.Step 2: Check each option
Object.entries(data) uses the correct syntax. data.entries() is invalid because objects don't have an entries() method. Object.entry(data) uses a wrong method name. entries(Object.data) has incorrect syntax.Final Answer:
Object.entries(data) -> Option BQuick Check:
Correct method is Object.entries(obj) [OK]
- Calling entries as a method on the object
- Misspelling 'entries' as 'entry'
- Passing object incorrectly
const obj = {a: 1, b: 2};
console.log(Object.entries(obj));Solution
Step 1: Apply Object.entries to the object
Object.entries returns an array of arrays, each inner array has the key and value from the object.Step 2: Match output to options
For obj = {a:1, b:2}, output is [['a', 1], ['b', 2]] which matches [['a', 1], ['b', 2]] exactly.Final Answer:
[['a', 1], ['b', 2]] -> Option CQuick Check:
Object.entries(obj) = [['a', 1], ['b', 2]] [OK]
- Swapping keys and values
- Expecting a flat array of keys or values
- Confusing output format with JSON string
const user = {name: 'Alice', age: 25};
for (const [key, value] of user.entries()) {
console.log(key, value);
}Solution
Step 1: Identify method usage on object
Plain objects do not have an entries() method; Object.entries() is a static method on Object.Step 2: Correct usage
To iterate entries, usefor (const [key, value] of Object.entries(user))instead ofuser.entries().Final Answer:
user.entries() is not a function -> Option AQuick Check:
Objects lack entries() method; use Object.entries(obj) [OK]
- Calling entries() directly on object
- Confusing Object.entries with instance method
- Ignoring error message about missing function
const data = {x: 10, y: 0, z: null, w: 'hello'}, which code snippet correctly creates a new object with only entries having truthy values?Solution
Step 1: Filter entries with truthy values
Use Object.entries(data) to get pairs, then filter pairs where value is truthy (non-zero, non-null, non-false).Step 2: Convert filtered entries back to object
Use Object.fromEntries() on filtered array to create new object with only truthy values.Final Answer:
Object.fromEntries(Object.entries(data).filter(([k, v]) => v)) -> Option AQuick Check:
Filter entries then fromEntries to object [OK]
- Forgetting to convert back to object
- Using map instead of filter
- Trying to call filter on object directly
Ready to prove it?
Pass the test and this pattern moves unknown
