'Search in All' using LinqJs

3

I'm using linqJs

When I try to get the id 5512 it returns null , but when I get the 124 , it works.

$.Enumerable.From(structure).Where("$.id==5512").ToArray();

Object structure:

[{
    "id": 124,
    "text": "Pai",
    "children": [
      {
        "id": 5511,
        "text": "Filho 1"
      },
      {
        "id": 5512,
        "text": "Filho 2"
      }
    ]
}]

How do I search for an ID regardless of the position it is in? In this case the 5512

  

I imagine that maybe I could do $.children.id but the array is dynamic so do not   I have how to know the exact position, so I would like to search at all id

    
asked by anonymous 11.04.2017 / 22:51

1 answer

3

You can not access because the item with ID 5512 is in a list within a list, ie, by doing .Where("$.id==5512") you are only looking at the parent elements and not children .

To do what you want, you can use .SelectMany() and after that, select by ID. See this example below that I think you will understand better.

var lista = [{
  "id": 124,
  "text": "Pai",
  "children": [{
      "id": 5511,
      "text": "Filho 1"
    },
    {
      "id": 5512,
      "text": "Filho 2"
    }
  ]
}];

var item = Enumerable.From(lista)
                     .SelectMany("$.children")
                     .Where("$.id==5512")
                     .ToArray();

console.log(item);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>
    
04.05.2017 / 19:03