从列表javascript中删除一个对象

时间:2016-12-19 08:39:42

标签: javascript

我有一棵树,我选择了一些节点并将其放入数组selectitems列表中,我有一个列表leftTree我要从leftTree中删除所选项目 但没有工作。selectitems.indexOf(obj.id)条件不起作用。我有  这是我的代码

var selectitems = $('#tree_2').jstree('get_selected', true);
    for(var i = 0; i < leftTree.length; i++) {
        var obj = leftTree[i];

        if(selectitems.indexOf(obj.id) !== -1) {
            leftTree.splice(i, 1);
            i--;
        }
    }


leftTree[{"text":"bbb","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"bbb","is_leaf":false,"id":"1","parent":"#"}, {"text":"Assets","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"bbb","is_leaf":false,"id":"2","parent":"1"},{"text":"Liability","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"bbb","is_leaf":false,"id":"3","parent":"1"},{"text":"Income","is_active":true,"parent_code":"bits","layer_code_length":10,              "layer_count":3,"name":"bbb","is_leaf":false,"id":"4","parent":"1"},{"text":"Expense","is_active":true,"parent_code":"bits","layer_code_length":10,"            layer_count":3,"name":"bbb","is_leaf":false,"id":"5","parent":"1"},{"text":"asset1","is_active":true,"parent_code":"1","layer_code_length":10,"laye            r_count":3,"name":"bbb","is_leaf":true,"id":"21","parent":"2"},{"text":"BANK","is_active":true,"parent_code":"1","layer_code_length":10,"layer_           count":3,"name":"bbb","is_leaf":true,"id":"24","parent":"5"},{"text":"liability1","is_active":true,"parent_code":"1","layer_code_length":10,"            layer_count":3,"name":"Brac IT Services   Limited","is_leaf":true,"id":"25","parent":"3"}, {"text":"OVERHFC","is_active":true,"parent_code":"1","layer_code_length":10,"lay        er_count":3,"name":"bbb","is_leaf":true,"id":"27","parent":"5"},  {"text":"expense1","is_active":true,"parent_code":"1","layer_code_length":10,"la       yer_count":3,"name":"bbb","is_leaf":true,"id":"29","parent":"5"}]


 selectitems:[{"id":"24","text":"BANK","icon":"fa fa-file icon-state-default","parent":"5","parents":["5","1","#"],"children":[],"children_d":[],"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"li_attr":{"id":"24"},"a_attr":{"href":"#"},"original":{"text":"BANK","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"bbb","is_leaf":true,"id":"24","parent":"5"},"type":"default"},{"id":"27","text":"OVERHFC","icon":"fa fa-file icon-state-default","parent":"5","parents":["5","1","#"],"children":[],"children_d":[],"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"li_attr":{"id":"27"},"a_attr":{"href":"#"},"original":{"text":"OVERHFC","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"bbb","is_leaf":true,"id":"27","parent":"5"},"type":"default"}]

2 个答案:

答案 0 :(得分:0)

您可以添加一个检查对象内部ID的函数,例如

Array.prototype.indexOfId = function(id) {
   for (var i = 0; i < this.length; i++)
      if (this[i].id === id)
        return i;
   return -1;
}

this solution

中指定

使用它你只需要改变

selectitems.indexOf(obj.id) !== -1

selectitems.indexOfId(obj.id) !== -1

答案 1 :(得分:0)

使用findIndex

var leftTree = [{"text":"Brac IT Services Limited","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":false,"id":"1","parent":"#"},{"text":"Assets","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":false,"id":"2","parent":"1"},{"text":"Liability","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":false,"id":"3","parent":"1"},{"text":"Income","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":false,"id":"4","parent":"1"},{"text":"Expense","is_active":true,"parent_code":"bits","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":false,"id":"5","parent":"1"},{"text":"asset1","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"21","parent":"2"},{"text":"BANK","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"24","parent":"5"},{"text":"liability1","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"25","parent":"3"},{"text":"OVERHFC","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"27","parent":"5"},{"text":"expense1","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"29","parent":"5"}]

var selectitems = [{"id":"24","text":"BANK","icon":"fa fa-file icon-state-default","parent":"5","parents":["5","1","#"],"children":[],"children_d":[],"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"li_attr":{"id":"24"},"a_attr":{"href":"#"},"original":{"text":"BANK","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"24","parent":"5"},"type":"default"},{"id":"27","text":"OVERHFC","icon":"fa fa-file icon-state-default","parent":"5","parents":["5","1","#"],"children":[],"children_d":[],"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"li_attr":{"id":"27"},"a_attr":{"href":"#"},"original":{"text":"OVERHFC","is_active":true,"parent_code":"1","layer_code_length":10,"layer_count":3,"name":"Brac IT Services Limited","is_leaf":true,"id":"27","parent":"5"},"type":"default"}];


function DeleteFromList(key, list, itemsToBeDeleted){
  itemsToBeDeleted.forEach((item)=>{
     var index = list.findIndex((obj) => obj[key] == item[key]);
    list.splice(index,1);
  });
}

console.log(leftTree.length)
DeleteFromList("id", leftTree, selectitems)
console.log(leftTree.length)