Objects – Methods (keys, values, entries)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Objects – Methods (keys, values, entries)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does Object.keys() return an array of an object's keys?
A No
B Yes
2. Does Object.values() return an array of an object's values?
A No
B Yes
3. Does Object.entries() return an array of key-value pairs?
A Yes
B No
4. Is 'object' with a lowercase o valid for these methods?
A No
B Yes
5. Do these methods need an object passed as an argument?
A Yes
B No

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Object.keys(person) AReturns an array of person's values
2Object.values(person) BWrong - 'Object' must start with a capital O
3Returns an array pairing each key with its value CReturns an array of person's keys
4Collects only the property names of an object DObject.entries(person)
5console.log(object.keys(a)); EWrong - 'keys' must start with a lowercase k
6console.log(Object.Keys(b)); FObject.keys(car)

Definitions

Object.keys()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
console.log(Object.____(person));
2.
console.log(Object.______(person));
3.
console.log(Object._______(person));
4.
let names = Object.____(car);
5.
let vals = Object.______(car);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A console.log(object.keys(a));
B let a = {x: 1};
C console.log(a);
Question 2
A let b = {y: 2};
B console.log(Object.Keys(b));
C console.log(b);
Question 3
A let c = {z: 3};
B console.log(c);
C console.log(Object.values(c));
Question 4
A console.log(Object.Values(d));
B let d = {a: 1};
C console.log(d);
Question 5
A let e = {b: 2};
B console.log(Object.entries(e));
C console.log(e);

Answer Key

Objects – Methods (keys, values, entries)
Section A: Quick Questions
Q1
B — Yes
Q2
B — Yes
Q3
A — Yes
Q4
A — No
Q5
A — Yes
Section B: Match the Pairs
Q1
Object.keys(person) → Returns an array of person's keys
Q2
Object.values(person) → Returns an array of person's values
Q3
Returns an array pairing each key with its value → Object.entries(person)
Q4
Collects only the property names of an object → Object.keys(car)
Q5
console.log(object.keys(a)); → Wrong - 'Object' must start with a capital O
Q6
console.log(Object.Keys(b)); → Wrong - 'keys' must start with a lowercase k
Section C: Definitions
Q1
Object.keys() — Returns an array containing all the keys of a given object.
Section D: Fill in the Blanks
Q1
console.log(Object.keys(person));
Q2
console.log(Object.values(person));
Q3
console.log(Object.entries(person));
Q4
let names = Object.keys(car);
Q5
let vals = Object.values(car);
Section E: Spot the Error
Q1
A — 'Object' must start with a capital O, not lowercase.
Q2
B — 'keys' must be lowercase - a capital K causes an error.
Q3
C — This line is actually correct - Object.values() works this way.
Q4
A — 'values' must be lowercase - a capital V causes an error.
Q5
B — This line is actually correct - Object.entries() works this way.