question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filter support for nested array (attributes)

See original GitHub issue

I am using the 1.48 version. I have a list that is created based on a factory function (two different kind of cells)

Let’s say the json like this (nested):

[
{ name: "a",age:"12"},
{ name: "b",age:"14"},
{ group: "groupB", people: {[ { name: "z",age:"14"}, { name: "s",age:"16"}] }},
{ name: "c",age:"16"},
{ group: "groupA", people: {[ { name: "e",age:"14"}, { name: "f",age:"16"}] }},
{ name: "d",age:"18"}
]

In the documentation there is no explanation for how to filter a nested result…

// returns entries without nested people
var oFilter = new Filter("name", FilterOperator.EQ, sName),

but how can I get the group back that contains also a Name value ?

// returns nothing
var oFilter = new Filter("people.name", FilterOperator.EQ, sName),

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
stopcodercommented, Jan 2, 2018

Hi @kyakubi ,

it’s not very clear whether your filter is applied to a JSONModel or a ODataV2Model. Therefore I will explain a bit about the nested filtering on both models.

The OData Version 2 supports filtering on navigation property only when the navigation property is NOT a collection.

The URL example in a reply from @ThePlenkov works because the relation between “Address” and one “Suppliers” is 1:1 and not N:1.

/Suppliers?$filter=Address/City eq ‘Redmond’

If the navigation property is a collection (like in your example), the OData Version 2 doesn’t support filtering on its property. Please see details here: https://stackoverflow.com/a/33218540

The filtering on a JSONListBinding doesn’t support the nested filtering either. You have to use a custom filtering function and filter the items manually as you have already mentioned in your last reply.

I didn’t get it to work with the native filters. So I created a custom filter (need a documentation update on that because its really lacking…

So for anyone that wants to filter through a tree you can use something like this to filter…

let oGroupFilter = new Filter(“”, oNode => { if(oNode.Group){ var match = oNode.Group.some(People=> { if (People.name == sName){ return true;} }); return match; } });

Best regards, Jiawei

2reactions
ThePlenkovcommented, Oct 20, 2017

@stephania87 As I understand, what @kyakubi wants is to use V2 filter like this:

/Suppliers?$filter=Address/City eq ‘Redmond’

This URL is described in version 2.0: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS (ES6): Filter array based on nested array attributes
You can use filter() with some() method to check if some of the objects in animals ...
Read more >
Filtering an Array of Nested Arrays and Objects Using Angular ...
The purpose of this function is to identify all the nested objects and and arrays in the parent object, delete the nested object...
Read more >
Nested filter by one item from array not every - Open Q&A
Hello, I have index of products, each product has variants. I am trying to filter by attributes of variants. Structure of product looks ......
Read more >
Nested Array: Filter on a String [] /Map doesn't return results as ...
Problem is i am not able to achieve the filter working properly. I did tried to have the below properties as Map and...
Read more >
Segment and filter against complex arrays and JSON
In the past, you weren't able to create segments or use complex JSON attributes (arrays, nested objects, and arrays of objects) in filters, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found