2

kyler decides to delete his account. Loop through your array of objects until you find kyler's account - use kyler@test.com 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: 'talon@test.com',
          password: 'test1234',
          username: 'tdog',
        },{
          name: 'lauren',
          email: 'lauren@test.com',
          password: 'admin1234',
          username: 'ldogg',
        },{
          name: 'cohen',
          email: 'cohen@test.com',
          password: 'baby1234',
          username: 'cdoggy',
        },{
          name: 'kyler',
          email: 'kyler@test.com',
          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]) === 'kyler@test.com') {
                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

StuffedPoblano
  • 499
  • 1
  • 8
  • 24
  • 1
    Possible duplicate of [How to remove a particular element from an array in JavaScript?](http://stackoverflow.com/questions/5767325/how-to-remove-a-particular-element-from-an-array-in-javascript) – Rathma Sep 02 '16 at 06:55
  • Just for clarification: Your if statement is wrong. First of all the indexOf method returns the index of an object and then you compare a number with the email string. Additionally you use [i], so you are looking for the index of an array containing the value of i which most definitely will return -1 since it won't find it in your array. The next problem is your slice call. slice returns a new array so let your function return it. Instead I would advice you to use splice like @Yury Tarabanko suggested. Nevertheless you should note that you don't call slice with an object but with the index i. – Urknecht Sep 02 '16 at 07:10
  • @Urknecht Thank you for your explanation. That helps me wrap my head around it. – StuffedPoblano Sep 02 '16 at 07:15

3 Answers3

6

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;
     }
  }
}
Yury Tarabanko
  • 39,619
  • 8
  • 73
  • 90
4

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 === 'kyler@test.com'
  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 === 'kyler@test.com') {
        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 == 'kyler@test.com'
}), 1);

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

users.splice(users.findIndex(function(v) {
  return v.email == 'kyler@test.com'
}), 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 == 'kyler@test.com') {
    // if true then remove the element and break the while loop
    users.splice(len, 1);
    break;
  }
}

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

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


console.log(users);
Pranav C Balan
  • 106,305
  • 21
  • 136
  • 157
2

You can use Array.prototype.findIndex() to find at what index is an element within an array by provided testing function (where in your case you will check for email property). Use Array.prototype.slice() to remove your object at that specific index.

Example below, as you can see User with a that specific email has been removed from the original array:

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

var removeUser = function(data, email) {
  var index = data.findIndex(function(item) {
    return item.email === email;
  });
  data.splice(index, 1);
};
removeUser(users,'kyler@test.com');
console.log(users);
GibboK
  • 64,078
  • 128
  • 380
  • 620