0

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    In other words you want to find objects in the response which have `title: 'Project'` set in them, but at *any* level within the data structure, is that correct? – Rory McCrossan Oct 18 '18 at 14:59
  • Exactly Rory McCrossan – Guilherme Felipe Reis Oct 18 '18 at 15:01
  • 2
    The problem has nothing to do with [JSON](http://json.org). `jsonData` is just a plain old object (therefor it's a misleading name) – Andreas Oct 18 '18 at 15:01
  • You state "filter" on the one hand and "if I have any title: 'project' in the Ajax response I do one thing if not I do another thing." These are totally different operations--the first transforms the data structure into something else (you should show expected output). The second is a search, which doesn't require modifying the data structure. Please clarify. – ggorlen Oct 18 '18 at 15:02
  • 1
    @Andreas indeed, I've edited it for the OP – Rory McCrossan Oct 18 '18 at 15:04
  • @ggorlen I want to check if I have "Node" with the title ´project´, so I thought to use filter for it. If there is any occurrence of project I will do something, but my Main goal here is to find if the response has an title project. – Guilherme Felipe Reis Oct 18 '18 at 15:04
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Heretic Monkey Oct 18 '18 at 15:06
  • 1
    @HereticMonkey I found that dupe target too, but it's not really applicable in this case as it's targeting infinite depth. Most dupes are only single level. – Rory McCrossan Oct 18 '18 at 15:09
  • @RoryMcCrossan Actually there's a section in the accepted answer titled "What if the "depth" of the data structure is unknown to me?". – Heretic Monkey Oct 18 '18 at 15:13
  • @HereticMonkey it helped me to understand better about this, however I don't think it can solve the case. I am trying to get information from there and solve the case – Guilherme Felipe Reis Oct 18 '18 at 15:28
  • 1
    @GuilhermeFelipeReis I don't think `filter` is really appropriate for a single node search--that seems like an [XY problem](https://en.wikipedia.org/wiki/XY_problem). If you want to simply find any `title` key matching some property, perform a [DFS](https://en.wikipedia.org/wiki/Depth-first_search) and return once you've found the key or exhausted the search space. Can you edit your question to remove all of the AJAX code? That seems incidental and [gets in the way](https://stackoverflow.com/help/mcve) of offering you a working solution. – ggorlen Oct 18 '18 at 15:40

0 Answers0