Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Filtering Nested Array Object to Use in FlatList

I have a FlatList that renders following data successfully.

const messages = [
  {
    "user": {
      "id": 4,
      "userId": "1561652136"
    },
    "resultsCount": 9,
    "results": [
      {
        "id": 252,
        "senderId": 147,
        "recipientId": 4,
        "body": "some text"
      },
      {
        "id": 253,
        "senderId": 147,
        "recipientId": 4,
        "body": "some other text"
      }
    ]
  },
  {
    "user": {
      "id": 14,
      "userId": "6155616153"
    },
    "resultsCount": 17,
    "results": [
      {
        "id": 275,
        "senderId": 147,
        "recipientId": 145,
        "body": "can you find also this text"
      },
      {
        "id": 276,
        "senderId": 145,
        "recipientId": 147,
        "body": "you should find this text"
      }
    ]
  }
]

I want to filter messages according to user input. So, I am trying to achieve search functionality.

if (userInput) {
  const newData = messages.filter(message => {
    const messageData = message ? message.toUpperCase() : ''.toUpperCase()
    const textData = text.toUpperCase()
    return messageData.indexOf(textData) > -1
  })
  setFilteredData(newData)
} else {
  setFilteredData(messages)
}

Above code is not working since user should only be able to search in the body.

For example if the userInput is can, newData should be like that:

[
  {
    "user": {
      "id": 14,
      "userId": "6155616153"
    },
    "resultsCount": 17,
    "results": [
      {
        "id": 275,
        "senderId": 147,
        "recipientId": 145,
        "body": "can you find also this text"
      },
      {
        "id": 276,
        "senderId": 145,
        "recipientId": 147,
        "body": "you should find this text"
      }
    ]
  }
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's one way to aggregate the bodies of each message's results and test them for the presence of a given text. This assumes the availability of the messages array, per your original posted code.

function findMessages(text) {
  return messages.filter(function(message) {
    const bodies = message.results.map(result => result.body).join('\n');
    return bodies.includes(text);
  });
}

console.log(findMessages("can"));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda