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

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

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 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!