首页 > 解决方案 > 如何从我的 Vue 项目中的数组中删除对象

问题描述

如何从数组中删除对象?

array = [{id:"1", name:"Jhon"},
         {id:"2", name:"Kabir"},
         {id:"3", name:"Rasel"}];

我想找到 ID 号,然后删除该对象。我该怎么做呢?

标签: javascriptvue.js

解决方案


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


推荐阅读