Search code examples
alasql

Alasql select in arrary of array with uknown index


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?


Solution

  • 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]);