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"}]
asked Dec 19, 2016 at 8:39
3
  • Can you post the contents of both arrays (i.e. leftTree and selectitems) here? Commented Dec 19, 2016 at 8:41
  • It won't work, to find index of object on the basis of property. You need to use findIndex method. refer this - developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Dec 19, 2016 at 8:42
  • I gave my two lists there Commented Dec 19, 2016 at 8:53

2 Answers 2

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
answered Dec 19, 2016 at 8:58
Sign up to request clarification or add additional context in comments.

Comments

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)

answered Dec 19, 2016 at 9:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.