I have an object retrieved using AJAX. The AJAX will return an object with unlimited nodes and each node can have his own children. I would like to filter this object by Title
, so I can get only the title projects.
My idea is, if I have any title: 'project'
in the response I do one thing if not I do another thing.
With the snippet below how could I get this multilevel object and filter it? I tried with filter
but it didn't work. I was trying to get grep
working, but I must be doing something wrong, because it also doesn't work. I am open for ES5/ES6 and TS solutions.
$.mockjax({
url: '/node/initdata',
responseTime: 100,
contentType: 'application/json',
responseText: {
"id": "33",
"collapsed": false,
"name": "Plan",
"title": "BusinessPlan",
"children": [{
"id": "266",
"collapsed": false,
"name": "addd",
"title": "BusinessPlanObjGoal",
"children": [{
"id": "65",
"collapsed": false,
"name": "Adding information ",
"title": "Kpi",
"children": [],
"due": 7
}, {
"id": "99",
"collapsed": false,
"name": "addd",
"title": "Project",
"children": []
}]
}, {
"id": "267",
"collapsed": false,
"name": "adddasdas",
"title": "BusinessPlanObjGoal",
"children": [{
"id": "55",
"collapsed": false,
"name": "Adding new item",
"title": "Kpi",
"children": [],
}]
}, {
"id": "268",
"collapsed": false,
"name": "plan 2",
"title": "BusinessPlanObjGoal",
"children": [{
"id": "299",
"collapsed": false,
"name": "Teste Gui",
"title": "BusinessPlanObjGoal",
"children": []
}]
}]
}
});
$.ajax({
url: '/node/initdata',
type: 'GET',
dataType: 'json',
statusCode: {
400: function() {
alert('Wrong Format!!!');
}
},
complete: function(data, status) {
jsonData = data.responseJSON;
console.log(jsonData);
/*
dt = cdt.filter(function (entry){
return entry.title==='department manager';
});
console.log(dt);
*/
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.min.js"></script>