1

I want to filter the data in a FlatList of react-native,

I have several filters,

The first is easy a word, the second filter (independent of the previous one) a list (array) of tags.

The data

Array [
  Object {
    "ID": 1,
    "name": "Name 1",
    "tags": Array [
      Object {
        "ID": 1,
        "name": "tag 1",
      },
      Object {
        "ID": 2,
        "name": "tag 2",
      },
    ],
  },
  Object {
    "ID": 2,
    "name": "Name 2",
    "tags": Array [
      Object {
        "ID": 1,
        "name": "tag 1",
      },
    ],
  },
]

The React Native component

searchedText = "string" works well & selectedTags = [] like key tags described above

<FlatList
  data={ 
    filterType === null ? items
      : filterType === "text" ? items.filter(item => item.name.includes(searchedText))
      : filterType === "tags" ? items.filter(item => item.tags.includes(selectedTags))
      : null
  }
  keyExtractor={( item ) => item.ID.toString() }
  renderItem={ ({ item }) => <AnotherComponent item={ item }/> }
/>

I do not know how to work deeply, Do you have a solution to filter the selected tag(s)?

Like :

items.filter(item => item.tags.includes([{name:"tag1"},{name:"tag2"}]))
2
  • This article can help you. medium.freecodecamp.org/… Commented Dec 17, 2018 at 14:14
  • Thank you for this paper, but it does not help me, I do filter give them but I do not know how to do it deep Array > Array Commented Dec 18, 2018 at 3:42

1 Answer 1

1

You can do it like this:

items.filter(item => item.tags
  .some(tag => selectedTags
    .map(selectedTag => selectedTag.name)
    .includes(tag.name)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.