Javascript angularjs如何通过下划线或其他方式从数组中删除对象
我的问题看起来与其他问题相似,但事实并非如此 请看一看,了解我的情况 我有一个对象数组,看起来像这样Javascript angularjs如何通过下划线或其他方式从数组中删除对象,javascript,arrays,angularjs,underscore.js,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:
$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"
}
现在我必须从这些数组中删除一个对象,如下所示
$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+): 使用下划线.js,您可以执行以下操作:
myArray = _(myArray).filter(function(obj) {
return obj.$$hashKey!== selectedObj.$$hashKey;
});
这应该可以让您使用相当标准的javascript(IE9+): 使用下划线.js,您可以执行以下操作:
myArray = _(myArray).filter(function(obj) {
return obj.$$hashKey!== selectedObj.$$hashKey;
});
您可能想使用函数,它基本上与dustmouse的答案相同,但我认为代码更可读
var selectedObj = {...}
$scope.events = [0,1,...,n];
$scope.events = _.remove($scope.events, function(element) {
return element == selectedObj;
});
您可能想使用函数,它基本上与dustmouse的答案相同,但我认为代码更可读
var selectedObj = {...}
$scope.events = [0,1,...,n];
$scope.events = _.remove($scope.events, function(element) {
return element == selectedObj;
});
所选事件可以通过此过程删除。调用函数deleteEvent
$scope.deleteEvent= function() {
var index = $scope.events.indexOf(selectedObj);
$scope.events.splice(index, 1);
};
所选事件可以通过此过程删除。调用函数deleteEvent
$scope.deleteEvent= function() {
var index = $scope.events.indexOf(selectedObj);
$scope.events.splice(index, 1);
};