๐ Scenario: You have a small online store. You keep a list of products and their prices in an object.You want to see each product with its price printed out clearly.
๐ฏ Goal: Build a program that uses Object.entries() to get product names and prices, then prints each product with its price.
๐ What You'll Learn
Create an object called products with exact entries: 'apple': 1.2, 'banana': 0.5, 'orange': 0.8
Create a variable called entries that stores Object.entries(products)
Use a for loop with variables product and price to iterate over entries
Print each product and price in the format: Product: apple, Price: 1.2
๐ก Why This Matters
๐ Real World
Objects are used to store related data like product lists, user info, or settings in many programs.
๐ผ Career
Understanding how to work with objects and their entries is essential for web development and JavaScript programming jobs.
Progress0 / 4 steps
1
Create the products object
Create an object called products with these exact entries: 'apple': 1.2, 'banana': 0.5, 'orange': 0.8
Javascript
Hint
Use curly braces {} to create an object and separate entries with commas.
2
Get entries from the products object
Create a variable called entries that stores Object.entries(products)
Javascript
Hint
Use Object.entries() to get an array of key-value pairs from the object.
3
Loop over the entries
Use a for loop with variables product and price to iterate over entries
Javascript
Hint
Use for (const [product, price] of entries) to get each product and price.
4
Print each product and price
Inside the loop, print each product and price in the format: Product: apple, Price: 1.2
Javascript
Hint
Use console.log with a template string to print the product and price.
Practice
(1/5)
1. What does Object.entries(obj) return when used on an object obj?
easy
A. A new object with the same keys and values
B. An array of values only
C. An array of keys only
D. An array of [key, value] pairs from the object
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 D
Quick Check:
Object.entries = array of [key, value] pairs [OK]
Hint: Remember: entries give key-value pairs as arrays [OK]
Common Mistakes:
Thinking it returns an object
Confusing entries with keys or values only
Expecting a flat array instead of pairs
2. Which of the following is the correct syntax to get entries from an object data?
easy
A. entries(Object.data)
B. Object.entries(data)
C. Object.entry(data)
D. data.entries()
Solution
Step 1: Recall the correct method name and usage
The correct method is Object.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 B
Quick Check:
Correct method is Object.entries(obj) [OK]
Hint: Use Object.entries(obj), not obj.entries() [OK]