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"
}
]
[type=TP & value>30] (works with json-query)
[type=TP & value>30 & type = UU with value > 90](not working with json-query)
The syntax of creating object using object literal is given below: object={property1:value1,property2:value2..... propertyN:valueN}
$() The $() function is shorthand for the getElementByID method, which, as noted above, returns the ID of a specific element of an HTML DOM. It's frequently used for manipulating elements in a document. $() allows for shorter and more efficient JavaScript coding. Traditional method: document.
[object, object] is the string representation of a JavaScript object data type. You'll understand better as we go further in this article. There are two main contexts where you'll encounter such an output: When you try display an object using the alert() method (most common).
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With