1console.log("hello-world);
console.log("hello-world);
.container {
width: 80%;
}
<pre><code class="language-css">
.container {
width: 80%;
}
</code></pre>
1console.log("hello-world);
console.log("hello-world);
.container {
width: 80%;
}
<pre><code class="language-css">
.container {
width: 80%;
}
</code></pre>
When working with JavaScript Map and Set objects, it's common to need to check if they are empty. Lets explore different ways to achieve this in JS.
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.
// Map
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
// Set
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
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
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.
Problem (JavaScript)
function 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:
tasks
Map will have at most 100 entries.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🚀!
Feel free to reach out!