How to use foreach loop in Node Js with example

While working on node application, we generally need to traverse and manipulate object’s element. Here in this example we will see how can use foreach loop in Node Js.

As we know that we can do socket programming using node. So here we will take an example of socket. Foreach loop is an easiast way to traverse through all elements. 

Example:

socket.on("newmessage", function(obj) {
  obj = JSON.parse(obj);
  obj.forEach(function(items) {
      console.log(items.userid);
      var e = "notify-"+items.userid;
      io.sockets.emit(e, 1);
  });
});