skip to Main Content

I have a pretty simple setup like this:

var itemsPerAssignee = {}

My plan is to save the amount of items in this object, so I can later iterate over it and, depending on the key, output the specific value on how many items are left for that specific user.

In my for-loop I iterate like this, to fill the object:

query.forEach(function (task) {
    var assignee = task.getAssignee();
    itemsPerAssignee[assignee] = {count: 0}
}

this at least doesn’t yield an error, but my output doesn’t seem to update. So I basically would need to check, if there is a value already existing for that specific key. If so, I need to increment it by 1.

I know I could have a function to check that specific index of the object, read the value, adjust and write it. But that doesn’t seem clean. I’m pretty sure there is an easier way to do so, but I don’t know how.

I also fiddled around with something like:

itemsPerAssignee = {user: assignee, count: 0}

but I got stuck there and I don’t know if my approach was correct.

Also, right now, I have objects with the same key multiple times, which might be part of the problem. Can someone point me in the right direction?

var itemsPerAssignee = {};
var users = ["[email protected], [email protected]", "[email protected]"];

for(u in users) {
    itemsPerAssignee[u] = {count: 0};
}

for(i in itemsPerAssignee) {
    console.log(itemsPerAssignee[i].count);
}

5

Answers


  1. Just check if exists, then create or update

     if(itemsPerAssignee.hasOwnProperty(assignee)){
         itemsPerAssignee[assignee].count++;
     }else{
         itemsPerAssignee[assignee] = {count: 1};
     }
    

    If assignee is an actual value (like an id or email, a unique key) and not an object…

    Login or Signup to reply.
  2. Since you want to count the number of items for each assignee in your task list and store it in the object, but here you are not incrementing the count, the correct code to increment items will be:

    const itemsPerAssignee = {};
    // taking users array just for demonstration
    const users = ["[email protected]", "[email protected]", "[email protected]"];
    
    for (let i = 0; i < users.length; i++) {
        let assignee = users[i];
    
        // Check if the assignee key already exists in itemsPerAssignee
        if (itemsPerAssignee.hasOwnProperty(assignee)) {
            // If it exists, increment the count
            itemsPerAssignee[assignee].count += 1;
        } else {
            // If it doesn't exist, create a new entry with count 1
            itemsPerAssignee[assignee] = { count: 1 };
        }
    }
    
    // Check the result
    for (let key in itemsPerAssignee) {
        console.log(key, itemsPerAssignee[key].count);
    }
    
    Login or Signup to reply.
  3. You can use the Nullish coalescing assignment:

    itemsPerAssignee[assignee] ??= { count: 0 }; // initialise if not present
    itemsPerAssignee[assignee].count++; // update
    
    Login or Signup to reply.
  4. Just for fun and giggles, here’s an one-liner solution using Object.groupBy which has the worst compatibility that no one should use:

    const users = ["[email protected]", "[email protected]", "[email protected]"];
    
    const tasks = Object.fromEntries(Object.entries(Object.groupBy(users, _ => _))
      .map(([k, { length }]) => [k, { count: length }])
    );
    
    console.log(tasks);
    Login or Signup to reply.
  5. Can be done with one line:

    (itemsPerAssignee[assignee] ??= { count: 0 }).count++;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search