I do it this way but at the output I get in groups empty
groups.forEach(function (groupe) { if (typeof groupe != 'array') { groupe = []; } if (groupe.length < 2) { groups.push(groupe.push(socket.id)); } }); Maybe it can not be so, for those who do not understand a little I will finish to complete the picture
for(var i=1;i<10;i++){ groups.forEach(function (groupe) { if (typeof groupe != 'array') { groupe = []; } if (groupe.length < 2) { groups.push(groupe.push(i)); } }); } I expect to receive [[1,2],[3,4],[5,6],[7,8],[9,10]]
In general, I did so but I do not think that this is the best option =)
var put = false; var find = false; groups.forEach(function (groupe, key) { var index = groupe.indexOf(socket.id); if (index != -1) { find = true; return false; } }); if (!find) { groups.forEach(function (groupe, key) { if (groupe.length < 2) { groupe.push(socket.id); put = true; } }); if (!put) groups.push([socket.id]); } So then I delete from groups, not sure whether it will work for groups of 3 or more people, that is, if you replace the limit in the group
function ClearGroups(groupes, users) { groupes.forEach(function (groupe,key) { groupe.forEach(function (user) { var index = users.indexOf(user,groupe_key); if (index == -1) { groupe.splice(groupe_key, 1); } }); if(groupe.length == 0){ groupes.splice(key, 1); } }); }