0

I have a tree I selected some nodes and put it in a list of arrayselectitemsand i have a list leftTree I want to remove selected items from my leftTree but not working.selectitems.indexOf(obj.id) condition is not working. i have Here is my code

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"}]
USERRR5
  • 430
  • 2
  • 10
  • 27
  • Can you post the contents of both arrays (i.e. `leftTree` and `selectitems`) here? – BenM Dec 19 '16 at 08:41
  • It won't work, to find index of object on the basis of property. You need to use findIndex method. refer this - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex – Chetan Dec 19 '16 at 08:42
  • I gave my two lists there – USERRR5 Dec 19 '16 at 08:53

2 Answers2

0

You can add a function that checks the ID inside the object, like

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

as specified in this solution

to use it you just have to change

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

into

selectitems.indexOfId(obj.id) !== -1
Community
  • 1
  • 1
Giovazz89
  • 391
  • 3
  • 12
0

use 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)
A.T.
  • 24,694
  • 8
  • 47
  • 65