Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
How to update 2D Array in Javascript
var memberName = input.question("Please enter member's name: ");
        memberName = memberName.substring(0, 1).toUpperCase() + memberName.substring(1).toLowerCase();
        subMemberGroups();
        if (subMemberGroup.includes(memberName)) {
            const memberGroupIndex = memberGroup.findIndex((e) => e.name === memberName)
            if (memberGroupIndex !== -1) {
                memberGroup.splice(memberGroupIndex, 1); // if exists remove it
                console.log("Member deleted!\n");
            } else {
                console.log("Member does not exist.\n");
            }
        } 
        else {
            console.log("Member does not exist.\n");
        }
        choices();
while (true) {
            var memberName = input.question("Please enter member's name: ");
            memberName = memberName.substring(0, 1).toUpperCase() + memberName.substring(1).toLowerCase();
            subMemberGroups();
            if (subMemberGroup.includes(memberName)) {
                console.log ("\nMember's name exists in database. Please enter a new name.");
            }
            else {
                var memberDOB = input.question("Please enter member's date of birth: ");
                var memberDJ = input.question("Please enter member's date joined: ");
                var userNew = new Member (memberName, 'Ruby', memberDJ, memberDOB, 0);
                memberGroup.push(userNew);
                break;
            }
        }
while (true) {
        var memberName = input.question("Please enter member's name: ");
        memberName = memberName.substring(0, 1).toUpperCase() + memberName.substring(1).toLowerCase();
        subMemberGroups();
        if (subMemberGroup.includes(memberName)) {
            console.log ("\nMember's name exists in database. Please enter a new name.");
        }
function subMemberGroups() {
    for (var i = 0; i < memberGroup.length; i++) {
        subMemberGroup.push(memberGroup[i]["name"]);
    }
}
memberGroup = [userJohn, userLemuel];
subMemberGroup = ['John', 'Samuel'];
var userJohn = ['John', '20'];
var userLemuel = ['Lemuel', '22'];
var userJohn = new Member ['John', '24'];

The first block of code will delete the details of either of them depending on what the user input. The code works fine. However, after trying to add new member, it prompts me that the member already exist. How do I permanently delete the user details in the 2D array?

The second block of code will add new member. However, after the first block of code executes, the second block of code says that the name already existing although I have already ran the first block od code to delete the member i.e John.

I was wondering what can I do to resolve this?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

When you remove a member, you remove him from memberGroup but he is still in subMemberGroup, when you try to add / remove a member, you first check inside subMemberGroup if he is already existing

Try edit like this :

if (subMemberGroup.includes(memberName)) {
    const memberGroupIndex = memberGroup.findIndex((e) => e.name === memberName)
    if (memberGroupIndex !== -1) {
        memberGroup.splice(memberGroupIndex, 1); // if exists remove it
        subMemberGroup.splice(subMemberGroup.indexOf(memberName), 1); // add this line to remove member from subMemberGroup

        console.log("Member deleted!\n");
    } else {
        console.log("Member does not exist.\n");
    }
} 
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda