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>In JavaScript, objects and maps are powerful data structures that allow you to store key-value pairs. When working with these data structures, you may need to increment a value associated with a specific key. In this guide, weβll explore various methods to increment a value in an object or a map using JavaScript.
To increment a value in an object, you can use dot or bracket notation. In the dot notation example, we directly access the property using a period, while in bracket notation, we use square brackets. This flexibility is handy, especially when dealing with keys that contain spaces or special characters.
// Using dot notation
obj.count = obj.count + 1 || 1;
console.log(obj.count); // ποΈ 2
// Using bracket notation
obj['count'] = obj['count'] + 1 || 1;
console.log(obj.count); // ποΈ 3Now, letβs introduce a more elegant approach using the nullish coalescing operator. Here, we utilize the nullish coalescing operator (??) to check if the property is defined. If it is, we increment it; otherwise, we initialize it to 1. Elegant, isnβt it?
const obj = {
  num: 1,
};
obj['num'] = (obj['num'] ?? 0) + 1;
console.log(obj.num); // ποΈ 2Now, letβs shift our focus to maps and discover how to gracefully increment values within this powerful data structure.
In this example, we leverage the set method of the Map class to update an existing keyβs value or create a new key if it doesnβt exist. The logical OR (||) ensures proper initialization.
const map1 = new Map([['num', 1]]);
  
map1.set('num', map1.get('num') + 1 || 1);
console.log(map1.get('num')); // ποΈ 2Enjoy the elegance of nullish coalescing in a map context:
Just like in the object scenario, the nullish coalescing operator ensures a smooth incrementation process, handling undefined or null values with grace.
const map1 = new Map([['num', 1]]);
  
map1.set('num', (map1.get('num') ?? 0) + 1);
console.log(map1.get('num')); // ποΈ 2In the spirit of Test Driven Development ( π), lets test our understanding by solving a problem.
Implement a function countDuplicates(map) that counts and returns the number of duplicate values in a map.
Problem (JavaScript)
function countDuplicates(map) {
  // > > > π Write code here π < < <
}
const sampleMap = new Map([
  ['a', 1],
  ['b', 2],
  ['c', 1],
  ['d', 3],
  ['e', 2],
]);
console.log(countDuplicates(sampleMap));
// Expected output π: 2Please attempt before seeing the Answer:
function countDuplicates(map) {
  const valueCount = new Map();
  // Count occurrences of each value in the map
  map.forEach((value) => {
      const count = valueCount.get(value) || 0;
      valueCount.set(value, count + 1);
  });
  // Count the number of values with occurrences greater than 1
  let duplicateCount = 0;
  valueCount.forEach((count) => {
      if (count > 1) {
          duplicateCount++;
      }
  });
  return duplicateCount;
}
// In this solution, we use an additional valueCount map to keep track of the occurrences of each value in the given map. We then iterate over the map, updating the count in valueCount for each value encountered.
// Finally, we iterate over valueCount to count the number of values that have occurrences greater than 1, indicating duplicates.
// Feel free to test the countDuplicates function with other maps to explore its functionality further. Happy coding! πConclusion:
There you have it! Incrementing values in JavaScript objects and maps is now well within your coding arsenal. Armed with dot and bracket notation, the nullish coalescing operator, and the mighty set method, youβre ready to conquer any coding challenge that comes your way.
Happy coding, and may your values always be on the rise! π
Feel free to reach out!