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
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 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,
arr[i].email === '[email protected]'Array#splice method, users.splice(i,1);.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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With