Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
Deleting values in 2D Array Javascript
userJohn = ('Leonardo', '22');
userLemuel = ('Catherine', '21',);
memberGroup = [userJohn, userLemuel];
subMemberGroup = ('John', 'Samuel');

var memberName = input.question("Please enter member's name: ");
        if (subMemberGroup.includes(memberName)) {
            for (var i = 0; i < 2; i++) {
                if (memberName == memberGroup[i]["name"]) {
                    for (let x in memberGroup[i]) {
                        memberGroup.splice(i, i);
                    }
                }
            }
            console.log ("Member deleted!\n");
        }
        else {
            console.log ("Member does not exist.\n");
        }

I have been trying to use slices to delete values in a 2D array.

What is happening is after the user inputs a name, the if statement will check if the user input name equals the name in the subMemberGroup. After which, the for loop will execute twice since there are only two values in the array, then the program will slice the 2D array that matches the name that was input by the user.

All in all, what I am trying to achieve is that I want the user to enter a name, and use the if statement to check the name matches any of the names in subMemberGroup. After which, use a for loop to loop through which array that has the name value 'name' entered by the user. Then proceed to slice the entire value in the array that belongs to the particular user, i.e. userJohn.

Is there any solution to this?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Here is a working example, remember that the arrays use "[]" brackets and if you want to have access to properties like "name", you have to define an object, which, in my opinion, would be a better approach.

const userJohn = ['John', '22'];
const userSamuel = ['Samuel', '21'];
const memberGroup = [userJohn, userSamuel];
const subMemberGroup = ['John', 'Samuel'];

var memberName = 'John';
if (subMemberGroup.includes(memberName)) {
  const memberGroupIndex = memberGroup.findIndex((e) => e[0] === memberName) // find and index of the element in the array and check if this element exists
  if (memberGroupIndex !== -1) { // check if element exists
    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");
}

console.log(memberGroup);

An example based on objects

const userJohn = {
  name: 'John',
  age: '22'
};
const userSamuel = {
  name: 'Samuel',
  age: '21'
};
const memberGroup = [userJohn, userSamuel];
const subMemberGroup = memberGroup.map((e) => e.name) // create array like this to make sure that it contains the nams that exists in memberGroup array
console.log(subMemberGroup);
var memberName = 'John';
if (subMemberGroup.includes(memberName)) {
  const memberGroupIndex = memberGroup.findIndex((e) => e.name === memberName) // find and index of the element in the array and check if this element exists (use "name" attribute instead of index)
  if (memberGroupIndex !== -1) { // check if element exists
    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");
}

console.log(memberGroup);

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda