๐ Scenario: You work in a small shop and keep track of products and their prices in an object. You want to see all the product names and their prices separately.
๐ฏ Goal: Learn how to get all keys and values from an object using JavaScript.
๐ What You'll Learn
Create an object called products with exact keys and values
Create a variable called productNames to hold the keys of products
Create a variable called productPrices to hold the values of products
Print both productNames and productPrices to see the lists
๐ก Why This Matters
๐ Real World
Objects are used to store related data like product lists, user profiles, or settings in many web applications.
๐ผ Career
Knowing how to access keys and values in objects is essential for frontend and backend developers working with data structures.
Progress0 / 4 steps
1
Create the products object
Create an object called products with these exact entries: "apple": 2, "banana": 1, "orange": 3.
Javascript
Hint
Use curly braces {} to create an object with keys and values.
2
Get the product names
Create a variable called productNames and set it to the keys of the products object using Object.keys(products).
Javascript
Hint
Use Object.keys() to get all keys from an object.
3
Get the product prices
Create a variable called productPrices and set it to the values of the products object using Object.values(products).
Javascript
Hint
Use Object.values() to get all values from an object.
4
Print the product names and prices
Write two console.log statements to print productNames and productPrices.
Javascript
Hint
Use console.log(variableName) to print the variables.
Practice
(1/5)
1. Which method returns an array of all the keys in a JavaScript object? const obj = {a: 1, b: 2};
easy
A. Object.values(obj)
B. Object.entries(obj)
C. Object.keys(obj)
D. Object.get(obj)
Solution
Step 1: Understand Object.keys()
Object.keys() returns an array of the object's own enumerable property names (keys).
Step 2: Compare with other methods
Object.values() returns values, Object.entries() returns key-value pairs, Object.get() is not a valid method.
Final Answer:
Object.keys(obj) -> Option C
Quick Check:
Keys = Object.keys(obj) [OK]
Hint: Keys come from Object.keys(), values from Object.values() [OK]
Common Mistakes:
Confusing Object.keys() with Object.values()
Using Object.entries() to get only keys
Trying to use Object.get() which doesn't exist
2. Which of the following is the correct syntax to get all values from an object const obj = {x: 10, y: 20};?
easy
A. Object.values(obj)
B. obj.values()
C. Object.keys(obj)
D. Object.getValues(obj)
Solution
Step 1: Identify the method for values
Object.values(obj) returns an array of the object's values.
Step 2: Check syntax validity
obj.values() and Object.getValues() are not valid JavaScript methods.
Final Answer:
Object.values(obj) -> Option A
Quick Check:
Values = Object.values(obj) [OK]
Hint: Use Object.values() to get all values from an object [OK]
Plain objects do not have a keys() method; Object.keys() is a static method.
Step 2: Identify the error type
Calling data.keys() causes a TypeError: data.keys is not a function.
Final Answer:
data.keys() is not a function -> Option D
Quick Check:
data.keys() causes TypeError [OK]
Hint: Use Object.keys(obj), not obj.keys() [OK]
Common Mistakes:
Calling keys() directly on object instance
Confusing Object.keys() with instance method
Expecting keys() to return values
5. Given the object const scores = {alice: 10, bob: 15, charlie: 0, dave: null};, which code snippet correctly creates an array of keys whose values are truthy?
hard
A. Object.values(scores).filter(value => value)
B. Object.keys(scores).filter(key => scores[key])
C. Object.entries(scores).filter(([k,v]) => k).map(([k,v]) => v)
D. Object.keys(scores).map(key => scores[key])
Solution
Step 1: Understand filtering keys by truthy values
We want keys where their values are truthy (non-zero, non-null).
Step 2: Analyze each option
Object.keys(scores).filter(key => scores[key]) filters keys by checking if scores[key] is truthy, returning keys correctly. Object.values(scores).filter(value => value) returns values, not keys. Object.entries(scores).filter(([k,v]) => k).map(([k,v]) => v) filters by keys (always truthy) and maps to values, wrong. Object.keys(scores).map(key => scores[key]) maps keys to values without filtering.
Final Answer:
Object.keys(scores).filter(key => scores[key]) -> Option B
Quick Check:
Filter keys by truthy values = Object.keys(scores).filter(key => scores[key]) [OK]
Hint: Filter keys by checking their values truthiness [OK]
Common Mistakes:
Filtering values but expecting keys
Mapping keys to values without filtering
Filtering keys by keys instead of values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.