我想通過ID的鍵/值從數組中刪除一個對象。我通常只是通過索引進行拼接,但是索引可能會發生很大的變化,因爲多個用戶將會操縱和更新對象,所以我想要更具體地描述一些東西 - 也就是id。所以我有一些邏輯來檢查它是否仍然存在,如果是這樣,請將其刪除。但是我似乎無法得到相當正確的語法。我正在使用underscore.js,我不知道它是否更容易,但它值得一提。從數組中刪除一個對象的ID(角度)
這裏是我有 -
$scope.toggleSelection = function(typeId,name,index){
//check if exists in array
check = $scope.multipleTypes.some(function(el) {
return el.name === name;
});
//checked on/off actions
if($scope.items[index].checked == false || $scope.items[index].checked == undefined){
//IS cecked
if(check){
//already exists, do nothing
}else{
$scope.multipleTypes.push({id:typeId, name:name, checked: true});
}
}else{
//IS not checked
if(check){
var list = _.filter($scope.multipleTypes, function(element){
return element.id != typeId;
}
$scope.multipleTypes = list;
}else{
//is not there, do nothing
}
}
};
所以,如果確實存在,並檢查了,它就會被推開。如果它存在並且未選中,我想通過它的ID將它從$ scope.multipleTypes中移除。我想我做錯了,我想要做的就是從$ scope.multipleTypes中刪除一個具有匹配ID的對象。將不勝感激任何幫助。謝謝閱讀!