for in loop behaving strangely in javascript -


  var buddy = ['2', '3']; console.log (friend); var room = []; (Different friends in friends) {var room = 'userid' + friend + 'friend'; console.log (rooms); Rooms.push (rooms); } Console.log (cell); User ID - Video Friends User's Friend ['userid0friends', 'userid1friends']   

This output

  ['2', '3'] User ID- / Code>  

3 is completely neglected, and this is my node. JS behaves more odd on the server

friends here's your array Index, there is no value on index

  var room = 'userid' + friends [friends] + 'friends';   

Plus when looping through an array, I do not recommend using for..in loops, you can see the Array.prototype.map You can use or limit the length of your array for the loop

Map of the array example:
  var friend = ['2', '3']; Console.log (friend); Var room = friends.map (function (friend, index) {return 'userid' + friends + 'friends';}); Console.log (rooms);   

Plain for loop:
  var buddies = ['2', '3']; Console.log (friend); Var room = []; (Var i = 0, l = friends.length; i & lt; l; ++ i) {var rooms = 'user-id' + friends [ii] + 'friends'; Console.log (rooms); Rooms.push (rooms); } Console.log (cell);    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -