javascript - angularjs如何通过下划线或其他任何方式从数组中删除对象

标签 javascript arrays angularjs underscore.js

我的问题看起来与其他问题很相似,但事实并非如此!

请看一下了解一下我的情况。

我有一个看起来像这样的对象数组

$scope.events =[
    {
        $$hashKey: "009"
        _allDay:false
        _id:5
        allDay:false
        className:[]
        end:Date {Fri Aug 08 2015 12:30:00 GMT+0530 (IST)}
        start:Date {Fri Aug 08 2015 12:30:00 GMT+0530 (IST)}
        title:"Birthday Party"
    },
    {
        $$hashKey:"006"
        _id:2
        end:Date {Wed Aug 05 2015 00:00:00 GMT+0530 (IST)}
        start:Date {Sun Aug 02 2015 00:00:00 GMT+0530 (IST)}
        title:"Long Event"
    },
    {
        $$hashKey:"007"
        _id:3
        allDay:false
        id:999
        start:Date {Fri Aug 07 2015 13:00:00 GMT+0530 (IST)}
        title:"Angular Event"
    },
    {
        $$hashKey:"008"
        _id:4
        allDay:false
        id:999
        start:Date {Tue Aug 11 2015 16:00:00 GMT+0530 (IST)}
        title:"Repeating Event"
    },
    {
        $$hashKey:"00A"
        _id:6
        end:Date {Sat Aug 29 2015 00:00:00 GMT+0530 (IST)}
        start:Date {Fri Aug 28 2015 00:00:00 GMT+0530 (IST)}
        title:"Click for Google"
    }
]

现在我必须从这些数组中删除一个对象,如下所示

var selectedObj = {
    $$hashKey:"009"
    _allDay:false
    _id:5
    allDay:false
    className:[]
    end:Date {Fri Aug 07 2015 12:30:00 GMT+0530 (IST)}
    start:Date {Fri Aug 07 2015 12:00:00 GMT+0530 (IST)}
    title:"Birthday Party"
}

我在做什么

removedArray = _.reject($scope.events, function(event) {
   return event.$$hashKey == selectedObj.$$hashKey
});

$scope.events = removedArray;

$scope.events 未更新我已尝试 $apply 但未成功。

有人可以帮我找出我做错了什么吗?

最佳做法是什么?这种脏东西。

最佳答案

这应该可以让你使用相当标准的 javascript (IE9+):

var index = myArray.map(function(e) { return e.$$hashKey; }).indexOf(selectedObj.$$hashKey);

if(index != -1) {
    myArray.splice(index, 1);

使用 underscore.js,您可以执行以下操作:

myArray = _(myArray).filter(function(obj) {
     return obj.$$hashKey!== selectedObj.$$hashKey;
});

关于javascript - angularjs如何通过下划线或其他任何方式从数组中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877676/

相关文章:

javascript - 如何在 React 中使用 CDN javascript

php - 如何使用 SMARTY 创建带有多维数组的 HTML 表格?

php - 简单的 MySql 查询到单一连接

PHP str_replace 用数组中的随机替换来替换需要?

javascript - Angular : ionic. bundle.js :21157 TypeError: PubNub. 中的 Ionic Pubnub 聊天发布不是一个函数

javascript - imageSwap onClick 通过 javascript 未触发

javascript - 为什么 ng-click 不起作用?

javascript - 如何在 JavaScript 中使用 RegExp 从这个简单的字符串中提取数字?

javascript - 单击下一个和上一个按钮 FullCalendar 进入议程 View

javascript - AngularJs 动态下载响应