Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Objects as Keys

The ability to use objects as keys is a significant feature of Maps.

Example

// Create Objects
const apples = {name: ‘Apples’};
const bananas = {name: ‘Bananas’};
const oranges = {name: ‘Oranges’};

// Create a Map
const fruits = new Map();

// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);

Keep in mind: the key is an object (apples), not a string (“apples”).

Example

fruits.get(“apples”);  // Returns undefined