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

Not iterating over map in node js

I am not able to get why my program isn’t logging in to the console in the show function, even though I tried 2 methods to iterate over a map. Here is the code,

var my_task = new Map();

const add = (priority, task_name) =>{
    if(my_task.has(priority) == false){
        my_task[priority] = new Array(); 
    }
    my_task[priority].push(task_name);
}

const init = () => {
    add(10, "Read");
    add(11, "Clarify doubts");
    add(7, "Play football");
};

const show = () => {
    console.log("Showing...\n", my_task);
    console.log("---");
    my_task.forEach((val, key) => {
        console.log(`${key} -> ${val}`);
    });
    console.log("---");
    for(let [key, val] of my_task){
        console.log(`${key} -> ${val}`);
    }
    console.log("Done");
};

init();
show();

Output:

D:\Docs(D)\z-imocha\javascript>node play.js
Showing...
 Map(0) {
  '7': [ 'Play football' ],
  '10': [ 'Read' ],
  '11': [ 'Clarify doubts' ]
}
---
---
Done

Can someone help explain why values in the map aren’t getting printed? and how to do it correctly. Thanks in advance.

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

>Solution :

You’re mixing up the syntax for Maps and plain objects.

Either use plain objects, and look up and assign properties with bracket notation:

const add = (priority, task_name) =>{
    if(!my_task[priority]){
        my_task[priority] = []; // don't use new Array
    }
    my_task[priority].push(task_name);
}
var tasksByPriority = {};

const add = (priority, task_name) =>{
    if(!tasksByPriority[priority]){
        tasksByPriority[priority] = [];
    }
    tasksByPriority[priority].push(task_name);
}

const init = () => {
    add(10, "Read");
    add(11, "Clarify doubts");
    add(7, "Play football");
};

const show = () => {
    Object.entries(tasksByPriority).forEach((val, key) => {
        console.log(`${key} -> ${val}`);
    });
};

init();
show();

Or use Maps, and use Map methods for everything:

const add = (priority, task_name) =>{
    if(!my_task.has(priority)){
        my_task.set(priority, []);
    }
    my_task.get(priority).push(task_name);
}

You also might consider using a more precise name than my_task – it’s a collection of tasks by priority, not a single task, so perhaps call it tasksByPriority.

var tasksByPriority = new Map();

const add = (priority, task_name) =>{
    if(!tasksByPriority.has(priority)){
        tasksByPriority.set(priority, []);
    }
    tasksByPriority.get(priority).push(task_name);
}

const init = () => {
    add(10, "Read");
    add(11, "Clarify doubts");
    add(7, "Play football");
};

const show = () => {
    tasksByPriority.forEach((val, key) => {
        console.log(`${key} -> ${val}`);
    });
};

init();
show();
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