Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!