Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to delete map entries using map.delete() function

I’m trying to delete the entry with key ‘1’ in the map. Can anyone tell me why is map.delete(1) returning false and the entry is not being deleted.

let nums = [1,1,1,2,2,3]
let map = new Map();
    for (let num of nums) {
        if (num in map) {
            map[num]++;
        } else {
            map[num] = 1;
        }
    }
    console.log(map) // Map(0) { '1': 3, '2': 2, '3': 1, '4': 3 }
    console.log(map.delete(1)); // false
    console.log(map) // Map(0) { '1': 3, '2': 2, '3': 1, '4': 3 }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let nums = [1,1,1,2,2,3, 4]
let map = new Map();
for (let num of nums) {
    if (num in map) {
        map.set(num, map.get(num)++);
    } else {
        map.set(num, 1)
    }
}
console.log(map)
console.log(map.delete(1))
console.log(map)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading