0

categories.location array is filtered like this:

  const displayedCategories = categories.filter(
    (category) => category.location.indexOf(selectedLocation) >= 0
  );

Now its structure is changed like below, there's additional nested values. How can I filter value/label now? category.location.value.indexOf doesn't work. Thanks.

"location" : [ 
    {
        "value" : "London",
        "label" : "London"
    }
             ]
1

1 Answer 1

1

Try:

const displayedCategories = categories.filter(
    (category) => category.location.some(loc=>loc.value==selectedLocation)
  );
Sign up to request clarification or add additional context in comments.

2 Comments

It's better to do category.location.some(loc => loc.value === selectedLocation) so you finish after the first match is found rather than looping through everything and building an array just so you can inspect the length.
@Jacob, good catch!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.