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

setFilter() specify key that's within an array of objects #8963

Closed
admly opened this issue Nov 10, 2019 · 4 comments
Closed

setFilter() specify key that's within an array of objects #8963

admly opened this issue Nov 10, 2019 · 4 comments

Comments

@admly
Copy link

admly commented Nov 10, 2019

I am trying to create a code that is setting filtering based on a key extracted from a GeoJson property which is an array of objects. As I know it is not possible.

So some feature has properties:

properties: {
      'someProperty': [
        {'something': 'A'}, {'something2':'B'}
       ]
    }

this.map.setFilter('points2', ['==', 'someProperty[0].something', 'A']));

Will there be any support for such a filtering? Maybe there is already some workaround for this? Or maybe I am using a wrong syntax?
Cheers

@ryanhamley
Copy link
Contributor

We recommend using the newer expression syntax instead of filters. What you're trying to do can be achieved with an expression.

Consider this example source that includes a property which is an array of objects:

"properties": {
    "someProp": [
        {"foo": "heyooo it works!"},
        {"foo": "b"},
        {"foo": "c"}
    ]
}

Now in our layer's layout properties, we can define the text field like this:

"text-field": ["get", "foo", ["at", 0, ["get", "someProp"]]],

This expression (reading from right to left) get the array at properties.someProp, then gets the element at the 0th index in the array, then gets property foo from that object. You would see heyooo it works! at the text on the map.

Hope this helps and thanks for using Mapbox!

@admly
Copy link
Author

admly commented Nov 12, 2019

@ryanhamley Thanks for answering. In meantime I actually modified my properties a little bit so it looks like this:

"properties": {
    "someProp" : {
         "A": ["X", "Y", "Z"],
         "B": ["W", "Y", "D"],
     }
}

Now:

['match', 'X', ['get', 'A', ['get', 'someProp']], true, false]));

actually returns false. Could you tell me what's wrong with this one?

@ryanhamley
Copy link
Contributor

ryanhamley commented Nov 12, 2019

@admly The way your expression is set up, it's getting someProp.A which is the array ["X", "Y", "Z"]. The expression then tries to match the string value "X" against the array value, which returns false. Think of it like this "X" === ["X", "Y", "Z"] // false.

What you want in this case is the new in expression operator which lets you determine if an item is in an array(or a substring is in a string).

['in', 'X', ['get', 'A', ['get', 'someProp']]]

This is equivalent to someProp.A.indexOf('X') >= 0.

Unfortunately, the in operator is only in the master branch right now. It will be in our beta release next week and included in v1.6.0 the week after that.

@admly
Copy link
Author

admly commented Nov 12, 2019

@ryanhamley YES! I tried to use the in operator, but there was an error in console (saying something that arrays are not supported or sth). But yea, I used 1.5.0 release, so the new feature was not included in it. THANK YOU FOR SUPPORT!!!

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

2 participants