-1

I am looking for equivalent code in javascript from JAVA code.

 for (Map.Entry<String, Participant> entry : room.getParticipants().entrySet()) {
         addParticipant(entry.getValue());
         break;
         }

Anyone can please suggests me what will be equivalent code in javascript?

Thanks

  • Welcome to Stackoverflow! Before make a question please use the search bar and make sure the question you're asking for has not been asked by anyone before. You're looking for this: http://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – FFdeveloper Apr 13 '17 at 18:03
  • Your best bet for getting to the point where you can start creating JavaScript versions of some of your Java code is to work through tutorials and books. This particular question was reasonably answerable with a couple of assumptions, but I suspect most of the other ones you'll have as you go forward will be too vague or broad for SO's format, and learning piecemeal like that probably wouldn't be all that useful to you anyway. Happy coding! – T.J. Crowder Apr 13 '17 at 18:13

1 Answers1

1

It depends entirely on what room.getParticipants() would return in the JavaScript version. But to try to point you the right direction in converting that code: You're probably going to want getParticipants to return either an ES2015 (aka "ES6") Map (if you can rely on ES2015+ features) or an object (typically used for what you use Map for in Java prior to ES2015).

If you're relying on ES2015+ features and have it return a Map, then

for (const entry of room.getParticipants().entries()) {
    addParticipant(entry[1]);
}

If you're limiting yourself to ES5 and earlier and have it return an object (typically used as maps prior to ES2015), then

var participants = room.getParticipants();
for (var key in participants) {
    addParticipant(participants[key]);
}

There's lots of great information (reference, tutorial, etc.) on the Mozilla Developer Network site.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875