Pietro
Pietro

Reputation: 1835

JS Object Query Syntax

I'm trying to find a way filter js collection with a query syntax similar to SQL.

The only library I found to accomplish this task is json-query.

It works in some cases but it has limitations. It isn't possible to query on different object levels with the same query or query for more than one result.

Here are some examples (Take the datastructure below as reference)

 [{
           "type": "UU",
            "value": "100",
            "tipo": "G",
            "strumento": "P",
            "aspetto": "C",
            "unit": "ml"
        },
        {
            "type": "PS",
            "value": "120/88",
            "rilevamento": "Manuale",
            "lato": "SX",
            "part": "Supina",
            "unit": "mmHg"
        },
        {
            "type": "TP",
            "value": "33.6",
            "tipo": "T",
            "unit": "°C"
        },
        {
            "type": "VO",
            "value": "12",
            "tipo": "VOAL",
            "unit": "ml"
        },
        {
            "type": "RS",
            "value": "60",
            "unit": "atti/min"
        },
        {
            "type": "HH",
            "value": "180",
            "modalita": "R",
            "unit": "cm"
        },
        {
            "type": "AA",
            "value": "50",
            "unit": "cm"
        },
        {
            "type": "PO",
            "value": "70",
            "rilevamento": "Manuale",
            "tipo": "FA",
            "sede": "PC",
            "unit": "bpm"
        }
    ]
  1. type = TP with value > 30

[type=TP & value>30] (works with json-query)

  1. type = TP with value > 30 AND type = UU with value > 90

[type=TP & value>30 & type = UU with value > 90](not working with json-query)

Upvotes: 6

Views: 1678

Answers (2)

Kamil Folwarczny
Kamil Folwarczny

Reputation: 591

By brief look on json-query page, I think your second query is wrong. Without * query will return only one record. Your query then doesnt make sense you cant have one item with two different type. I think your query should looks like this:

[* type=TP & value > 30 | type = UU & value > 90]

I can be wrong tho, i have never worked with that library.

Edit for comments

You cant do this with simle query, because every object is tested by your query and simply return bool value if it fits or not. You need to filter your data by first condition and the result filter with second condition.

var tempData = jsonQuery('[* type = TP & value > 30]', {data: data}).value;
var result =   jsonQuery('[* type = UU & value > 90]', {data: tempData }).value;

Edit - Possible solution

If you need to create query beforehand i would consider using array for storing single query and then apply them in sequence. I dont know if are you creating query in JS or on server so i wrote it in JS for code consistency.

var result;
var queryArray;

queryArray.push("[* type = TP & value > 30]");
queryArray.push("[* type = UU & value > 90]");

for (i = 0; i < queryArray.length; i++) {
    result = jsonQuery(queryArray[i], {data: result }).value;
}

Upvotes: 2

Dhaval Pankhaniya
Dhaval Pankhaniya

Reputation: 1996

i think you should use JsLinq

var myList = [
{FirstName:"Chris",LastName:"Pearson"},
{FirstName:"Kate",LastName:"Johnson"},
{FirstName:"Josh",LastName:"Sutherland"},
{FirstName:"John",LastName:"Ronald"},
{FirstName:"Steve",LastName:"Pinkerton"}    ];

var exampleArray = JSLINQ(myList)
.Where(function(item){ return item.FirstName == "Chris"; })
.OrderBy(function(item) { return item.FirstName; })
.Select(function(item){ return item.FirstName; });

Upvotes: 1

Related Questions