常见方法根据id删除数组中对应的项

第一种:使用filter()方法的方式。这种方法通过传入一个回调函数来过滤出不包含特定ID的对象,从而创建一个新的数组。

function removeObjectWithId(arr, id) {
  return arr.filter((obj) => obj.id !== id);
}

const arr = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Kate' },
  { id: 3, name: 'Peter' },
];

const newArr = removeObjectWithId(arr, 2);
// 输出: [ { id: 1, name: 'John' }, { id: 3, name: 'Peter' } ]
console.log(newArr);
// 原始数组未被修改
console.log(arr);

第二种:使用findIndex()splice()方法的方式。这种方法首先使用findIndex()方法找到具有特定ID的对象的索引,然后使用splice()方法从数组中删除该对象

function removeObjectWithId(arr, id) {
  const objWithIdIndex = arr.findIndex((obj) => obj.id === id);
  arr.splice(objWithIdIndex, 1);
  return arr;
}

const arr = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Kate' },
  { id: 3, name: 'Peter' },
];

removeObjectWithId(arr, 2);
// 输出: [ { id: 1, name: 'John' }, { id: 3, name: 'Peter' } ]
console.log(arr);

第三种:使用forEach()方法和临时数组来删除特定项

function removeObjectWithId(arr, id) {
  const newArr = [];
  arr.forEach((obj) => {
    if (obj.id !== id) {
      newArr.push(obj);
    }
  });
  return newArr;
}

const arr = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Kate' },
  { id: 3, name: 'Peter' },
];

const newArr = removeObjectWithId(arr, 2);
// 输出: [ { id: 1, name: 'John' }, { id: 3, name: 'Peter' } ]
console.log(newArr);
// 原始数组未被修改
console.log(arr);

第四中:使用reduce()方法来删除特定项

function removeObjectWithId(arr, id) {
  return arr.reduce((acc, obj) => {
    if (obj.id !== id) {
      acc.push(obj);
    }
    return acc;
  }, []);
}

const arr = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Kate' },
  { id: 3, name: 'Peter' },
];

const newArr = removeObjectWithId(arr, 2);
// 输出: [ { id: 1, name: 'John' }, { id: 3, name: 'Peter' } ]
console.log(newArr);
// 原始数组未被修改
console.log(arr);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较常见的需求,以下是一个简单的代码示例,可以实现你的要求: ```html <!DOCTYPE html> <html> <head> <title>动态生成表格</title> </head> <body> <table id="scoreTable"> <thead> <tr> <th>姓名</th> <th>科目</th> <th>成绩</th> <th>操作</th> </tr> </thead> <tbody> </tbody> </table> <button onclick="addRow()">添加行</button> <script> // 使用数组模拟数据 const data = [ { name: '张三', subject: '语文', score: 80 }, { name: '张三', subject: '数学', score: 90 }, { name: '李四', subject: '语文', score: 75 }, { name: '李四', subject: '数学', score: 85 }, ]; // 获取表格的 tbody 元素 const tbody = document.querySelector('#scoreTable tbody'); // 生成表格行的函数 function createRow(item) { const tr = document.createElement('tr'); tr.innerHTML = ` <td>${item.name}</td> <td>${item.subject}</td> <td>${item.score}</td> <td><a href="#" onclick="deleteRow(this)">删除</a></td> `; return tr; } // 初始化表格 data.forEach(item => { const tr = createRow(item); tbody.appendChild(tr); }); // 添加行的函数 function addRow() { const item = { name: '新人', subject: '英语', score: 70 }; const tr = createRow(item); tbody.appendChild(tr); } // 删除行的函数 function deleteRow(link) { const tr = link.parentNode.parentNode; tbody.removeChild(tr); } </script> </body> </html> ``` 这个示例中,我们先使用一个数组模拟了一些数据,并在页面加载时将数据渲染为表格。然后,我们提供了一个按钮,点击按钮就可以添加一行新的数据。同时,每行的最后一列提供了一个“删除”链接,点击链接就可以删除对应的行。注意,在删除行的时候,我们通过 `link.parentNode.parentNode` 来获取当前行的元素,再通过 `tbody.removeChild(tr)` 来删除这个元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值