[英]How to delete item from an array of object using angularjs?
i wanted to delete index from array if rest service response return true , so in below case lets say we have true from rest service i wanted to delete object where id is 'RA_ATTST_LANGUAGE` from array. 如果REST服务响应返回true,我想从数组中删除索引,因此在以下情况下,我们可以说REST服务具有true,我想从数组中删除id为'RA_ATTST_LANGUAGE'的对象。 I tried below code but its not deleting what is missing ? 我尝试下面的代码,但它不删除缺少什么?
main.js main.js
MessageAdminNotificationFactory.getAttestationLanValidation().then(function(response){
var data = response.data;
console.log('attestation',data);
if(data){
angular.forEach($scope.adminDataSource,function(value,$index){
if(value.id === 'RA_ATTST_LANGUAGE'){
$scope.adminDataSource.splice($index, 1);
}
console.log('dropdown',value.id);
});
}
});
$scope.adminDataSource = [{
"uid": null,
"index": 0,
"selected": null,
"expanded": null,
"id": "RA_PLTFRM_NOTIF",
"text": "Platform Maintenance Notification",
"parentId": null,
"items": null
}, {
"uid": null,
"index": 0,
"selected": null,
"expanded": null,
"id": "RA_ATTST_LANGUAGE",
"text": "Attestation Language",
"parentId": null,
"items": null
}]
$scope.adminDataSource = $scope.adminDataSource.filter(
function(value){
return value.id !== 'RA_ATTST_LANGUAGE';
})
Array.filter is the way to go. Array.filter是必经之路。 filters out anything that evaluates to false; 过滤掉所有评估为假的东西;
Iterate the array from end to start 从头到尾迭代数组
for(var i=$scope.adminDataSource.length-1;i>=0;i--){
var value = $scope.adminDataSource[i];
if(value.id === 'RA_ATTST_LANGUAGE') {
$scope.adminDataSource.splice(i,1);
}
}
This way it doesn't matter if the array shrinks while you iterate. 这样,在迭代时数组是否收缩并不重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.