Concept Flow - Object entries
Start with an object
Call Object.entries(obj)
Iterate over each key-value pair
Create array of [key, value
Return the array
This flow shows how Object.entries takes an object and returns an array of key-value pairs.
const obj = {a: 1, b: 2}; const entries = Object.entries(obj); console.log(entries);
| Step | Action | Object State | Result | Output |
|---|---|---|---|---|
| 1 | Define obj | {a: 1, b: 2} | Object created | |
| 2 | Call Object.entries(obj) | {a: 1, b: 2} | Array of entries created | [['a', 1], ['b', 2]] |
| 3 | Print entries | [['a', 1], ['b', 2]] | Output to console | [['a', 1], ['b', 2]] |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| obj | undefined | {a: 1, b: 2} | {a: 1, b: 2} | {a: 1, b: 2} |
| entries | undefined | undefined | [['a', 1], ['b', 2]] | [['a', 1], ['b', 2]] |
Object.entries(obj) returns an array of [key, value] pairs from obj.
Only own enumerable properties are included.
Useful for looping over object entries.
Result is an array of arrays.
Example: {a:1} → [['a',1]]Object.entries(obj) return when used on an object obj?data?Object.entries() called with the object as argument.const obj = {a: 1, b: 2};
console.log(Object.entries(obj));const user = {name: 'Alice', age: 25};
for (const [key, value] of user.entries()) {
console.log(key, value);
}for (const [key, value] of Object.entries(user)) instead of user.entries().const data = {x: 10, y: 0, z: null, w: 'hello'}, which code snippet correctly creates a new object with only entries having truthy values?Pass the test and this pattern moves unknown