Home > Back-end >  Deleting values in 2D Array Javascript
Deleting values in 2D Array Javascript

Time:07-18

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 slice to delete values in a 2D array.

What is happening is after user inputs a name, the if statement will check if the user input name equals to the name in the subMemberGroup. After which, the for loop will execute twice since there is 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, use if statement to check the name matches any of the name 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?

CodePudding user response:

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

  • Related