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

Posted

技术标签:

【中文标题】.splice() 正在从数组中删除 2 个对象而不是 1 个【英文标题】:.splice() is removing 2 objects from array instead of 1 【发布时间】:2018-03-12 01:08:17 【问题描述】:

例如,当输入“John Smith”时,slice 会删除前两个员工姓名,而不是只删除 John 的姓名。知道为什么会这样吗?

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

【问题讨论】:

【参考方案1】:

您可以使用filter 而不是forEach 使事情变得更简单:

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

如果你仍然想使用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);

【讨论】:

【参考方案2】:

杰基杰克逊仍在名单中

您可以像这样遍历列表:

1
2
3
4
5

对于第一次迭代,您位于索引 0。然后删除索引0 (John Smith)。 此时 Jackie Jackson 是新的索引0 但迭代会跳转到下一个元素(索引1),Chris Jones 是什么。

新的索引 0 永远不会注销到控制台!但他还在名单上!

【讨论】:

【参考方案3】:

您可以使用findIndex 查找名称与提示输入相同的对象的索引。使用该索引,您可以使用 splice 从员工数组中删除项目

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)

【讨论】:

【参考方案4】:

forEach 中不需要第三个参数。只需简单的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)

【讨论】:

【参考方案5】:

只需使用 Array#filter 函数即可删除项目。您无需先检查 (iteration),然后使用 forEach(iteration) 循环。您有 2 次迭代。您只能在一次迭代中执行此操作。

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

【讨论】:

以上是关于.splice() 正在从数组中删除 2 个对象而不是 1 个的主要内容,如果未能解决你的问题,请参考以下文章

我想通过 splice() 从数组中删除一个对象但不能正常工作

js数组删除某个元素

使用 splice() 从数组中删除匹配条件的项目

js数组操作

javascript基础06

js 数组遍历时删除元素