Create tree List
///////////////////////Tree Creation///////////////
///////////////////////Tree Creation///////////////
///////////////////////Tree Creation///////////////
function setRoot(obj: any, i ? : any, k ? : any) {
obj['root'] = i ? (i + '_' + k) : '0';
obj['expand'] = true;
if (obj.child) {
obj.child.forEach((val, key) => {
setRoot(val, obj['root'], key);
});
}
}
///////////////////////Tree Search////////////////
///////////////////////Tree Search////////////////
///////////////////////Tree Search////////////////
console.clear();
var items = [
{
name: "a1",
id: 1,
children: [{
name: "a2",
id: 2,
children: []
},
{
name: "a3",
id: 3,
children: []
}]
},
{
name: "a11",
id: 1,
children: [{
name: "a22",
id: 2,
children: []
},
{
name: "a33",
id: 3,
children: []
}]
}
];
function filterData(data, name) {
var r = data.filter((obj, kk)=>{
if (obj.children) {
if(obj.children.length >0){
obj.children = filterData(obj.children, name);
console.log(obj);
if(obj.children.length == 0){
return false;
}else{
return true;
}
}else{
return obj.name == name;
console.log(obj);
}
}else{
return obj.name == name;
}
})
return r;
}
console.log(filterData(items, 'a2'));
Comments
Post a Comment