6

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)

0

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

The query you mention works, but what if you want to return the results only if both exists (TP and UU) with corresponding values?
how can you return object where both TP and UU exists when your objects have only one type and one value? Or do you want to return whole list if it contains objects with these values?
In your example the result is: [ { type: "TP", value: "33.6", tipo: "T", unit: "°C" }, { type: "VO", value: "12", tipo: "VOAL", unit: "ml" } ]. With the OR condition both they match. I want the same with the AND condition. Of course the query sintax must be different, for example ([type=TP & value > 30] & [type=VO & value > 10]). No results if one of them is not present.
"Return whole list if it contains objects", this also could be an acceptable solution.
That's is actually something I wanted to avoid. Consider that could be more complex, maybe with 3 different types o more, mixed with AND/OR conditions. I would like to be able to firstly create the query string with some params without writing code. The solution should be: 'select * where Cond1 & Cond2'
|
1

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

3 Comments

JSLINQ isnt' a query string.
you want to filter records on query string bases ?
Yes, I want to be able to create a query string that I can execute on a data collection. Like: jsonQuery('[type=TP & value>30]', data)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.