简体   繁体   English

.splice()从数组中删除2个对象而不是1

[英].splice() is removing 2 objects from array instead of 1

When typing in 'John Smith' for example, slice removes the first two employee names instead of only John's. 例如,当输入'John Smith'时,slice会删除前两个员工姓名,而不是仅删除John的员工姓名。 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.'); } 

You could make things a little simpler using filter instead of forEach : 您可以使用filter而不是forEach使事情变得更简单:

if (employeeExists(removeEmployee)) {   
    employees = employees.filter(e => e.name !== removeEmployee);
}

If you still want to use splice , you could use findIndex with it: 如果您仍想使用splice ,可以使用findIndex

 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); 

Jackie Jackson still in the list 杰基杰克逊仍在名单中

You loop through the list like this: 你像这样遍历列表:

1
2
3
4
5

for the first iterration you are at index 0 . 对于第一次iterration你在索引0 Then you remove index 0 (John Smith). 然后删除索引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 . 此时 Jackie Jackson是新的指数0 ,但iterration跳转到下一个元素(索引1 ),什么是Chris Jones

The new index 0 is never logged out to the console! 新索引0永远不会登出到控制台! But he is still in the list! 但他仍然在名单中!

Simply use the Array#filter function to remove the items. 只需使用Array#filter功能即可删除这些项目。 You don't need first to check ( iteration ) and then loop with forEach ( iteration ). 您不需要先检查( 迭代 )然后使用forEach循环( 迭代 )。 You have 2 iterations. 你有2次迭代。 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); 

You can use findIndex to find the index of the object where name is same as the input of the prompt. 您可以使用findIndex查找对象的索引,其中name与提示的输入相同。 Using that index you can use splice to remove item from employees array 使用该索引,您可以使用splice从employees数组中删除项目

 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) 

You do not need third third parameter in forEach . 你不需要forEach第三个第三个参数。 Just simply splice the employees array as below. 只需简单地splice employees数组,如下所示。

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM