0
0
Javascriptprogramming~10 mins

Iterating over objects in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to loop over the keys of the object.

Javascript
const obj = {a: 1, b: 2, c: 3};
for (const [1] in obj) {
  console.log([1]);
}
Drag options to blanks, or click blank then click option'
Aitem
Bvalue
Ckey
Dindex
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'value' instead of 'key' in the for...in loop variable.
Trying to use 'for...of' directly on an object.
2fill in blank
medium

Complete the code to get the values of the object using Object.values().

Javascript
const obj = {x: 10, y: 20};
const values = Object.[1](obj);
console.log(values);
Drag options to blanks, or click blank then click option'
Avalues
Bkeys
Centries
Dget
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using Object.keys() instead of Object.values().
Trying to call a non-existent method like Object.get().
3fill in blank
hard

Fix the error in the code to correctly iterate over object entries.

Javascript
const obj = {a: 1, b: 2};
for (const [key, [1]] of Object.entries(obj)) {
  console.log(key, [1]);
}
Drag options to blanks, or click blank then click option'
Aentry
Bitem
Cindex
Dvalue
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'item' or 'index' which do not represent the value.
Trying to destructure incorrectly.
4fill in blank
hard

Fill both blanks to create a new object with keys in uppercase and values doubled.

Javascript
const obj = {a: 1, b: 2};
const newObj = Object.fromEntries(
  Object.entries(obj).map(([[1], [2]]) => [[1].toUpperCase(), [2] * 2])
);
console.log(newObj);
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Citem
Dentry
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Swapping key and value in destructuring.
Using unclear variable names like 'item' or 'entry'.
5fill in blank
hard

Fill all three blanks to filter object entries with values greater than 10 and create a new object.

Javascript
const obj = {a: 5, b: 15, c: 25};
const filteredObj = Object.fromEntries(
  Object.entries(obj)
    .filter(([[1], [2]]) => [2] [3] 10)
);
console.log(filteredObj);
Drag options to blanks, or click blank then click option'
Akey
Bvalue
C>
D<
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '<' instead of '>' in the filter condition.
Swapping key and value in destructuring.