Complete the code to create an empty hash map (object) in JavaScript.
const hashMap = [1];In JavaScript, an empty object {} is used to create a hash map (key-value store).
Complete the code to add a key-value pair to the hash map.
hashMap[[1]] = 'apple';
Keys in JavaScript objects are strings or symbols. To add a key 'fruit', use quotes: 'fruit'.
Fix the error in accessing the value for key 'fruit' from the hash map.
const value = hashMap[1]'fruit';
To access a value by key in an object, use square brackets with the key as a string: hashMap['fruit'].
Fill both blanks to check if the key 'fruit' exists in the hash map and print the value.
if ([1] [2]) { console.log(hashMap['fruit']); }
The in operator checks if a key exists in an object: 'fruit' in hashMap.
Fill all three blanks to create a hash map from an array of fruits with their counts, only including fruits with count greater than 1.
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']; const fruitCount = {}; for (const [1] of fruits) { fruitCount[[2]] = (fruitCount[[3]] || 0) + 1; } const result = {}; for (const fruit in fruitCount) { if (fruitCount[fruit] > 1) { result[fruit] = fruitCount[fruit]; } }
We use the variable fruit to loop through the array and update counts in the hash map.
