admin管理员组

文章数量:1356416

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

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

Share Improve this question asked Sep 2, 2016 at 6:50 StuffedPoblanoStuffedPoblano 6952 gold badges12 silver badges37 bronze badges 3
  • 1 Possible duplicate of How to remove a particular element from an array in JavaScript? – Rathma Commented Sep 2, 2016 at 6:55
  • Just for clarification: Your if statement is wrong. First of all the indexOf method returns the index of an object and then you pare 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 Commented Sep 2, 2016 at 7:10
  • @Urknecht Thank you for your explanation. That helps me wrap my head around it. – StuffedPoblano Commented Sep 2, 2016 at 7:15
Add a ment  | 

3 Answers 3

Reset to default 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;
     }
  }
}

In your code, the if statement would be always false since indexOf method returns an integer value and you are paring 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);

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: '[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 removeUser = function(data, email) {
  var index = data.findIndex(function(item) {
    return item.email === email;
  });
  data.splice(index, 1);
};
removeUser(users,'[email protected]');
console.log(users);

本文标签: javascriptDeleting a user in an ArrayStack Overflow