Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays with Objects #17

Open
PaulKruger opened this issue Nov 9, 2016 · 4 comments
Open

Arrays with Objects #17

PaulKruger opened this issue Nov 9, 2016 · 4 comments

Comments

@PaulKruger
Copy link

I didn't notice whether you had or planned to develop functions that included arrays that contained objects.

So for example if wanted to return true if there is an apple pie in group of pies:
var rules = { "and" : [
{"<" : [ { "var" : "temp" }, 110 ]},
{"==" : [ { "var" : "pies.filling" }, "apple" ] }
] };

var data = { "temp" : 100, "pies" : [{ "filling" : "apple" }, { "filling" : "cherry" }]};

jsonLogic.apply(rules, data);
// true

@jwadhams
Copy link
Owner

You could build a custom operaton using dotty's search method and then using that with in, like

jsonLogic.add_operation('var_search', function(key){
  return dotty.search(this, key); //In custom operations "this" is bound to data
});

var rules = { "and" : [
  {"<" : [ { "var" : "temp" }, 110 ]},
  {"in" : ["apple", { "var_search" : "pies.*.filling" } ] }
] };

var data = { "temp" : 100, "pies" : [{ "filling" : "apple" }, { "filling" : "cherry" }]};

jsonLogic.apply(rules, data);
// true

Does that work?

@internalfx
Copy link

Had the same problem.

This is a common use case, and should be added to core.

@jwadhams thank you for the solution.

@internalfx
Copy link

@PaulKruger please see #25

@yarnball
Copy link

Thanks for your example above.

I am trying to filter on multiple nested fields. However, it isn't returning any results. Any suggestions?

var rules = { "and" : [
  {"<" : [ { "var" : "temp" }, 110 ]},
  {"in" : ["apple", { "var_search" : "pies.*.filling" } ] },
  {"in" : [10, { ">" : "pies.*.score" } ]}
] };

var data = { "temp" : 100, "pies" : [{ "filling" : "apple", "score": 10 }, { "filling" : "cherry", "score": 7 }]};

jsonLogic.apply(rules, data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants