To make rules dynamic, pass a data object as the second argument to jsonLogic.apply(). Use the var operator to retrieve values from the data object.
Accessing object attributes:
jsonLogic.apply(
{ "var" : ["a"] }, // Rule
{ a : 1, b : 2 } // Data
);
// 1
Syntactic sugar for unary operators:
You can skip the array wrapper for unary operators like var:
jsonLogic.apply(
{ "var" : "a" },
{ a : 1, b : 2 }
);
// 1
Accessing array indices:
Use var with a numeric index to access elements in an array:
jsonLogic.apply(
{"var" : 1 },
[ "apple", "banana", "carrot" ]
);
// "banana"
Complex mixed rule example:
var rules = { "and" : [
{"<" : [ { "var" : "temp" }, 110 ]},
{"==" : [ { "var" : "pie.filling" }, "apple" ] }
] };
var data = { "temp" : 100, "pie" : { "filling" : "apple" } };
jsonLogic.apply(rules, data);
// true
jsonLogic.apply(
{ "var" : ["a"] },
{ a : 1, b : 2 }
);