我正在尝试使用LoDash从数组中删除对象。

var roomList = [
    {id: '12345', room: 'kitchen'},
    {id: '23456', room: 'lounge'},
    {id: '34567', room: 'bathroom'},
];

console.log(roomList); //outputs all 3 rooms
_.pull(roomList, {id: '12345'});
console.log(roomList); //STILL outputs all 3 rooms!


我以为_pull在这种情况下会起作用,但事实并非如此。如何使用LoDash从数组中删除对象?

最佳答案

试试这个代码:

var roomList = [
    {id: '12345', room: 'kitchen'},
    {id: '23456', room: 'lounge'},
    {id: '34567', room: 'bathroom'},
];

console.log(roomList);
_.pull(roomList, _.find(roomList, {id: '12345'}));
console.log(roomList);

10-06 08:20