albino montella
albino montella

Reputation: 33

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?

Upvotes: 3

Views: 1672

Answers (2)

agershun
agershun

Reputation: 4107

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.

Upvotes: 2

mathiasrw
mathiasrw

Reputation: 618

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

Upvotes: 2

Related Questions