javascript - 如何从我的 Vue 项目中的数组中删除对象
问题描述
如何从数组中删除对象?
array = [{id:"1", name:"Jhon"},
{id:"2", name:"Kabir"},
{id:"3", name:"Rasel"}];
我想找到 ID 号,然后删除该对象。我该怎么做呢?
解决方案
array = [{id:"1", name:"Jhon"},
{id:"2", name:"Kabir"},
{id:"3", name:"Rasel"}];
console.log(remove_by_id( array, '2' ));
function remove_by_id( array, id ) {
const index = array.findIndex( el => el.id === id );
if( index !== -1 )
array.splice( index, 1 )
return array;
}
推荐阅读
- php - 如何使用 PHP 从 HTML 中的 SQL LONGBLOB 中以任何格式显示图像
- lotus-notes - Lotusscript MIME 电子邮件
- docker-compose - “enablePrettyUrl”不适用于标准 docker-compose 配置
- javascript - 使用 mongoose.removeOne 后,Node JS throwing 在发送到客户端后无法设置标头
- html - 在列上设置相同的高度
- java - java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 7770 列路径 $.films[8].images.poster 处为 BEGIN_ARRAY
- javascript - 需要用回调比较两个数组并在Javascript中返回一个对象
- macos - 从具有特定命名选项卡目标的 osx 终端打开 chrome
- python - Python 中的分页(Beautifulsoup)
- python - Discord.py 机器人没有证书