Home > Enterprise >  Not iterating over map in node js
Not iterating over map in node js

Time:12-27

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.

CodePudding user response:

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

CodePudding user response:

This change in 'add' function will solve your problem.

const add = (priority, task_name) =>{

      if(my_task.has(priority) == false){
    
           my_task.set(priority,task_name);

     }

     // my_task[priority].push(task_name);

}
  • Related