This question already has answers here:
Remove Object from Array using JavaScript
                                
                                    (27个答案)
                                
                        
                                2年前关闭。
            
                    
为什么数组中有未定义的?如何删除对象?



arr = [
  {id:1,name:'aaa'},
  {id:2,name:'bbb'},
  {id:3,name:'ccc'}
];

for(var item in arr){
  if(arr.hasOwnProperty(item)){
    if(arr[item].id === 2){
      delete(arr[item]);
      continue;
    }
  }
}

console.log(arr);

最佳答案

希望这是您想要做的:-



var arr = [
  {id:1,name:'aaa'},
  {id:2,name:'bbb'},
  {id:3,name:'ccc'}
];


arr = arr.filter(function(item){
  return item.id != 2;
});

console.log(arr)

关于javascript - 如何删除对象? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46482287/

10-09 20:53