Below is the structure of the object:
var objList = [
{ "age": 19, "valueField": 34, "booleanField": false },
{ "age": 15, "valueField": 5, "booleanField": false },
{ "age": 22, "valueField": 17, "booleanField": true }
];
Given the condition below:
var condition = 'age > 18 && age < 30 && booleanField == true';
A filter function can be used as shown here:
var newObjList = objList.filter(function(obj) {
return obj.age > 18 && obj.age < 30 && obj.booleanField == true;
});
However, it's desired to use the condition directly without prefixing "obj." to each field.
objList.filter(function(obj) {
return conditon; // all the fields referenced in the condition should point to the corresponding obj fields
})
This approach resembles a SQL query:
SELECT *
FROM objlist
where " + condition + ";
The question arises: Is this achievable?