Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
loop a javascript function like in php foreach

I want to execute this function below 3 times but each time with a different value:

  socket.broadcast.to(role).emit('login', {
   onlineusers: list
  });

I have 3 roles: non-mods, mods and admins. The value for list varies for each role like this: non-mods => nonmodslist, mods => modslist, admins => adminslist

I tried:

 roles = {non-mods : nonmodslist, mods : modslist, admins : adminslist}
 
 for(let {non-mods, mods, admins} of roles) {
  socket.broadcast.to(role).emit('login', {
   onlineusers: list
  });

but I really dont know what to do from there. What am I missing?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use for of loop with Object.entries and deconstruction

let roles = {'non-mods': nonmodslist, 'mods': modslist, 'admins': adminslist};

for (let [role, list] of Object.entries(roles)) {
    socket.broadcast.to(role).emit('login', {
        onlineusers: list
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

for (var x = 0; x < roles.length; x++){
   socket.broadcast.to(roles[x]).emit.....
}

try it like this;

you want to set up an index (x) to iterate through roles, stopping once it reaches roles.length.... then you can access the specific role with that index

about 4 years ago · Juan Pablo Isaza Report

0

Please try below code

let roles = {'non-mods' : nonmodslist, 'mods' : modslist, 'admins' : adminslist}

for(let rolename in roles) {
  socket.broadcast.to(rolename).emit('login', {
  onlineusers: roles[rolename]
});
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!