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); } }); } 

    1 answer 1

    I do not really understand what the task is. If to abstract from groups and sockets what needs to be made?

    In addition, I note that Array.prototype.forEach needed to perform operations on all existing elements of the array. To search for a specific element, it is more correct to use Array.prototype.find . If you need to add a value to an array, then forEach not needed.

    Ie, for example, solving the puzzle "how to break the list of elements into groups of 2 elements" (get [[1,2],[3,4],[5,6],[7,8],[9,10]] ), you can apply this approach:

     /** * Adding the item to the list. Creates a new array, if there is no one with * enough space in it according to splitLimit. * * @item * @list - 2 dimension array */ function addItemToListWithSplitLimit(item, list, splitLimit) { var room = list.find(group => group.length < splitLimit) || []; room.push(item); addObject(list, room); return list; // or you could return result of room.push(item) } function createItemsListWithLimit(length, limit) { var items = getRangeFromZero(length); var list = []; var add = addItemToListWithSplitLimit; return items.reduce((list, item) => add(item, list, limit), list); } // Utils function getRangeFromZero(limit) { return Array.apply(null, Array(limit)).map((_, i) => i); } function addObject(array, object) { var isInArray = array.indexOf(object) !== -1; return isInArray ? object : array.push(object); } 

    Create such a list: createItemsListWithLimit(10, 2)

    • I try to enter the code and somehow apply it to objects, it is tight in the morning, that is, in fact, the group will store the user object with some name, id, glasses and other info. But thanks for the thank you, I did not use it before - Serge Esmanovich