javascript – How best to add users to groups

Question:

I do this but at the exit I get empty in groups

groups.forEach(function (groupe) {
    if (typeof groupe != 'array') {
        groupe = [];
    }
    if (groupe.length < 2) {
        groups.push(groupe.push(socket.id));
    }

});

Maybe this is not possible, for those who do not understand I will add a little to the completeness of 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));
        }

    });
}

Expect to receive [[1,2],[3,4],[5,6],[7,8],[9,10]]

In general, I did this, but I don't think it's 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 remove from the groups, I'm not sure whether it will work for groups of 3 or more people, that is, if you change 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);
        }
    });
}

Answer:

I don't really understand what the task is. If we abstract from groups and sockets, what should we do?

Also, note that Array.prototype.forEach needed to perform operations on all existing array elements. It is more correct to use Array.prototype.find to search for a specific element. If you need to add a value to an array, then forEach not needed.

That is, for example, solving the problem "how to split 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 a list like this: createItemsListWithLimit(10, 2)

Scroll to Top