When working with JavaScript’s Map and Set objects, it’s common to need to check if they are empty. Lets explore different ways to achieve this in JS.
Using the Size Property
Both Map and Set objects have a size
property, which represents the number of elements in the collection. Checking if this property is equal to zero is a straightforward way to determine if the Map or Set is empty.
const emptyMap = new Map();
const nonEmptyMap = new Map([[1, 'one'], [2, 'two']]);
console.log(emptyMap.size === 0); // Output: true
console.log(nonEmptyMap.size === 0); // Output: false
const emptySet = new Set();
const nonEmptySet = new Set([1, 2, 3]);
console.log(emptySet.size === 0); // Output: true
console.log(nonEmptySet.size === 0); // Output: false
This concise approach leverages the power of Array.from() to create a mapped array.
Checking for Existence of Elements
For Sets, you can directly check the existence of elements using the has method. For Maps, this involves checking if the size property is zero.
const emptyMap = new Map();
const nonEmptyMap = new Map([[1, 'one'], [2, 'two']]);
console.log(emptyMap.size === 0); // Output: true
console.log(nonEmptyMap.size === 0); // Output: false
const emptySet = new Set();
const nonEmptySet = new Set([1, 2, 3]);
console.log(emptySet.size === 0); // Output: true
console.log(nonEmptySet.size === 0); // Output: false
Using the forEach Method
Another approach is to use the forEach method to iterate through the elements. If the callback function is never called, it means the collection is empty. Weird, but yeah, can do.
function isEmpty(collection) {
let empty = true;
collection.forEach(() => (empty = false));
return empty;
}
const emptyMap = new Map();
const nonEmptyMap = new Map([[1, 'one'], [2, 'two']]);
console.log(isEmpty(emptyMap)); // Output: true
console.log(isEmpty(nonEmptyMap)); // Output: false
const emptySet = new Set();
const nonEmptySet = new Set([1, 2, 3]);
console.log(isEmpty(emptySet)); // Output: true
console.log(isEmpty(nonEmptySet)); // Output: false
🧪Practice Coding Problem: Task and Feedback Manager
In the spirit of Test Driven Development ( 😁), lets test our understanding by solving a problem.
You are building a small application to manage tasks and user feedback. Using JavaScript’s Map and Set, tasks are stored in a Map, and feedback is tracked with a Set.
Write a function
hasFeedback
that takes the tasks (Map) and feedback (Set) as parameters, along with a task ID. The function should return true if the specified task has received feedback and false otherwise.JavaScriptfunction hasFeedback(tasks, feedback, taskId) { // > > > 👉 Write code here 👈 < < < } const tasks = new Map([ [1, { description: 'Complete assignment' }], [2, { description: 'Prepare presentation' }], ]); const feedback = new Set([1]); console.log(hasFeedback(tasks, feedback, 1)); // Output: true console.log(hasFeedback(tasks, feedback, 2)); // Output: false
Constraints:
- Task IDs are positive integers.
- The
tasks
Map will have at most 100 entries.- The
feedback
Set will have at most 100 entries.
Please attempt before seeing the Answer:
function hasFeedback(tasks, feedback, taskId) {
// Check if the task ID exists in the tasks Map
if (tasks.has(taskId)) {
// Check if the task ID is in the feedback Set
return feedback.has(taskId);
} else {
// Task with the specified ID doesn't exist
return false;
}
}
// Example Usage:
const tasks = new Map([
[1, { description: 'Complete assignment' }],
[2, { description: 'Prepare presentation' }],
]);
const feedback = new Set([1]);
console.log(hasFeedback(tasks, feedback, 1)); // Output: true
console.log(hasFeedback(tasks, feedback, 2)); // Output: false
This solution defines a function hasFeedback
that takes a tasks
Map, a feedback
Set, and a taskId
as parameters. It checks if the task ID exists in the tasks Map and then checks if the task ID is in the feedback Set. The function returns true if the task has feedback and false otherwise.
OK, so if you are checking if a collection is empty, then just use the size
property or loop through elements. The only thing you can’t check is if the coffee pot in the office is empty—no JS magic for that! ☕ But I digressed, you keep coding🚀!