Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a user in an Array

kyler decides to delete his account. Loop through your array of objects until you find kyler's account - use [email protected] to find him in the array. Once you find the particular index he's located in, delete him from the array.

var users = [{
          name: 'talon',
          email: '[email protected]',
          password: 'test1234',
          username: 'tdog',
        },{
          name: 'lauren',
          email: '[email protected]',
          password: 'admin1234',
          username: 'ldogg',
        },{
          name: 'cohen',
          email: '[email protected]',
          password: 'baby1234',
          username: 'cdoggy',
        },{
          name: 'kyler',
          email: '[email protected]',
          password: 'delete1234',
          username: 'kdawg'
        }];

This is what I have so far, but I am spinning my wheels:

        function deleteUser(arr)
    for (var i = 0; i < users.length; i++) {
            if (arr.indexOf([i]) === '[email protected]') {
                users.slice(arr[i]);
            }
        }

I am not sure if each of my users needs a variable assigned in the array or if it is my slice. Any guidance would be awesome. Thanks

like image 526
StuffedPoblano Avatar asked Nov 29 '25 03:11

StuffedPoblano


2 Answers

Since you want to mutate original array you need splice

function deleteUser(arr, email) {
  for(var i = 0; i < arr.length; i++) {
     if(arr[i].email === email) {
       arr.splice(i, 1)
       return;
     }
  }
}
like image 119
Yury Tarabanko Avatar answered Dec 01 '25 16:12

Yury Tarabanko


In your code, the if statement would be always false since indexOf method returns an integer value and you are comparing it with a string using === so statement would be always false(type mismatch). And the second thing is slice method will not update the existing array it just makes a shallow copy of the array.

To make your own code to work do,

  1. Change if condition to arr[i].email === '[email protected]'
  2. Remove element using Array#splice method, users.splice(i,1);.
  3. After removing the element break the for loop by using break.

Final code :

 function deleteUser(arr)
   for (var i = 0; i < users.length; i++) {
     if (arr[i].email === '[email protected]') {
        users.slice(i, 1);
        break;
     }
   }
 }

Use Array#findIndex and Array#splice methods. Where Array#findIndex can be used for getting the element index (for older browser use simple loop to get the index) and Array#splice method for removing it using the index.

users.splice(users.findIndex(function(v) {
  return v.email == '[email protected]'
}), 1);

var users = [{
  name: 'talon',
  email: '[email protected]',
  password: 'test1234',
  username: 'tdog',
}, {
  name: 'lauren',
  email: '[email protected]',
  password: 'admin1234',
  username: 'ldogg',
}, {
  name: 'cohen',
  email: '[email protected]',
  password: 'baby1234',
  username: 'cdoggy',
}, {
  name: 'kyler',
  email: '[email protected]',
  password: 'delete1234',
  username: 'kdawg'
}];

users.splice(users.findIndex(function(v) {
  return v.email == '[email protected]'
}), 1);

console.log(users);

FYI : For older browser check polyfill option of findIndex method.


UPDATE : Or much faster and old browser supported version using a simple while loop;

var len = users.length;
// iterate upto `0`
while (len--) {
  // check the email  value
  if (users.email == '[email protected]') {
    // if true then remove the element and break the while loop
    users.splice(len, 1);
    break;
  }
}

var users = [{
  name: 'talon',
  email: '[email protected]',
  password: 'test1234',
  username: 'tdog',
}, {
  name: 'lauren',
  email: '[email protected]',
  password: 'admin1234',
  username: 'ldogg',
}, {
  name: 'cohen',
  email: '[email protected]',
  password: 'baby1234',
  username: 'cdoggy',
}, {
  name: 'kyler',
  email: '[email protected]',
  password: 'delete1234',
  username: 'kdawg'
}];

var len = users.length;
while (len--) {
  if (users[len].email == '[email protected]') {
    users.splice(len, 1);
    break;
  }
}


console.log(users);
like image 33
Pranav C Balan Avatar answered Dec 01 '25 15:12

Pranav C Balan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!