Reputation: 399
When typing in 'John Smith' for example, slice removes the first two employee names instead of only John's. Any idea why this is happening?
let removeEmployee = '';
let employees = [
{
name: 'John Smith'
}, {
name: 'Jackie Jackson'
}, {
name: 'Chris Jones'
}, {
name: 'Amanda Cullen'
}, {
name: 'Jeremy Goodwin'
}, ]
removeEmployee = prompt('Enter the name of the employee to be removed:');
function employeeExists(employee) {
return employees.some(function(el) {
return el.name === employee;
});
}
if (employeeExists(removeEmployee)) {
employees.forEach(function(employee, index, object) {
if (employee.name === removeEmployee) {
object.splice(index, 1);
} else {
console.log(employee.name);
}
});
} else {
console.log('That employee does not exist, please try again.');
}
Upvotes: 2
Views: 1515
Reputation: 1996
You loop through the list like this:
1
2
3
4
5
for the first iterration you are at index 0
. Then you remove index 0
(John Smith). At this point Jackie Jackson
is the new index 0
but the iterration jumps to the next element (index 1
), what is Chris Jones
.
The new index 0 is never logged out to the console! But he is still in the list!
Upvotes: 2
Reputation: 13356
You could make things a little simpler using filter
instead of forEach
:
if (employeeExists(removeEmployee)) {
employees = employees.filter(e => e.name !== removeEmployee);
}
If you still want to use splice
, you could use findIndex
with it:
let employees = [ {name: 'John Smith'}, {name: 'Jackie Jackson'}, {name: 'Chris Jones'}, {name: 'Amanda Cullen'}, {name: 'Jeremy Goodwin'} ];
var removeEmployee = 'Chris Jones';
var index = employees.findIndex(e => e.name === removeEmployee);
employees.splice(index, 1);
console.log(employees);
Upvotes: 3
Reputation: 68665
Simply use the Array#filter function to remove the items. You don't need first to check (iteration) and then loop with forEach
(iteration). You have 2 iterations. You can do it only during one iteration.
let employees = [
{ name: 'John Smith', },
{ name: 'Jackie Jackson' },
{ name: 'Chris Jones' },
{ name: 'Amanda Cullen' },
{ name: 'Jeremy Goodwin'}
];
let name = prompt('Enter the name of the employee to be removed:');
employees = employees.filter(emp => emp.name.localeCompare(name));
console.log(employees);
Upvotes: 1
Reputation: 3511
You do not need third third parameter in forEach
. Just simply splice
the employees
array as below.
let removeEmployee = '';
let employees = [{
name: 'John Smith'
}, {
name: 'Jackie Jackson'
}, {
name: 'Chris Jones'
}, {
name: 'Amanda Cullen'
}, {
name: 'Jeremy Goodwin'
}, ]
// let letters = ['a', 'd', 'c']
removeEmployee = prompt('Enter the name of the employee to be removed:');
function employeeExists(employee) {
return employees.some(function(el) {
return el.name === employee;
});
}
if (employeeExists(removeEmployee)) {
employees.forEach(function(employee, index) {
if (employee.name === removeEmployee) {
employees.splice(index, 1);
} else {
console.log(employee.name);
}
});
} else {
console.log('That employee does not exist, please try again.');
}
console.log(employees)
Upvotes: 1
Reputation: 50316
You can use findIndex
to find the index of the object where name is same as the input of the prompt. Using that index you can use splice
to remove item from employees array
let removeEmployee = '';
let employees = [{
name: 'John Smith'
}, {
name: 'Jackie Jackson'
}, {
name: 'Chris Jones'
}, {
name: 'Amanda Cullen'
}, {
name: 'Jeremy Goodwin'
}, ]
removeEmployee = prompt('Enter the name of the employee to be removed:');
function employeeExists(employee) {
let ifEmployee = employees.findIndex(function(el) {
return el.name === employee.trim();
})
return ifEmployee;
}
var employeIndex = employeeExists(removeEmployee);
if (employeIndex !== -1) {
employees.splice(employeIndex, 1)
} else {
console.log('That employee does not exist, please try again.');
}
console.log(employees)
Upvotes: 1