Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!