2

suppose I have this structure data

data =[ 
 {
"id":1,
"link":[
        {
        "id":3
        },
        {
        "id":1
        },
        {
        "id":2
        }
        ]
},
{
"id":2,
"link":[
         {
          "id":30
          },
         {
         "id":11
         },
         {
         "id":22
         }
         ]

}
]

I want see if my structure have a link with id=11

"SELECT * FROM ? WHERE link->[1]->id=11" 

work but because I already know that I must check in index 1. How can I check in all indexes?

Ian
  • 30,182
  • 19
  • 69
  • 107

2 Answers2

2

The SEARCH function would be good if it was fully implemented

alasql('SEARCH / link / WHERE(id=11) .. / .. FROM ?',[data]);

But the parrent .. selector is not implemented yet.

I suggest doing a (not totally elegant) user defined function:

alasql.fn.deepSearch = function(id, obj){
    return alasql("SEARCH / link / WHERE(id=?) FROM ?", [id, [obj]]).length
}

alasql('SELECT * FROM ? WHERE deepSearch(11,_)',[data]);
mathiasrw
  • 610
  • 4
  • 10
  • I replaced `[id, [obj])` with `[id, [obj]])` + included version 0.2.6 instad of version 0.1.9 in your code and it now spits out `[{"id":2,"link":[{"id":30},{"id":11},{"id":22}]}]` - http://jsfiddle.net/8g4k1hjo/3/ – mathiasrw May 03 '16 at 09:34
2

The correct code is

alasql('SEARCH / AS @a link / WHERE(id=11) @a FROM ?',[data]);

Here AS @a saves current element into the variable @a, and the second @a retrieves it.

agershun
  • 4,077
  • 38
  • 41