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

140
Views
React native filter array not working on string

I am having issues trying to filter an array of message objects. I do not want to include the objects with "_id" value "receiver" or "blockedUsers".

Array of message objects:

[
  {"_id":"receiver"},
  {"_id":"blockedUsers"},
  {"_id": MjIzx3XA1mpcuzgDVZj","createdAt":1631349363111,"text":"Ok","user":{"name":"Nikki","_id":"M6fBsPludfYVjJXKYvwgxHRacYw1"}},
  {"_id":" MjG3hFAgcNweJWh9SF7","createdAt":1631300277391,"text":"Again","user":{"name":"Chris","_id":"tFhmw5oQoPhk8nF2sx5rE5BFqw93"}
 }
]

The following doesn't seem to work

this.state.messages.filter(msg => !msg._id.includes("receiver") || !msg._id.includes("blockedUsers")).

The original array is returned.

But if I use this

this.state.messages.filter(msg => msg._id.includes("receiver") || msg._id.includes("blockedUsers"))

it returns:

[{"_id":"receiver"},{"_id":"blockedUsers"}]

Can you please assist?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The filter condition is wrong, it will always return true in your case.
Array.filter() callback should return true for the items you want to keep.

You have to use AND && instead of OR ||.

this.state.messages.filter(msg => 
!msg._id.includes("receiver") && !msg._id.includes("blockedUsers")
)

Why it initially returned true? Because when the id is "receiver", it doesn't include "blockedUsers" and the other way around. You want to keep the element if it doesn't include "receiver" AND it also doesn't include "blockedUsers".

Here is a truth table to make it more clear, it should be true for the items you want to keep.

!includes("receiver") !includes("blockedUsers") OR AND
false false false ✅ false ✅(skip item, it includes both strings)
false true true ❌ false ✅(skip item, it includes "blockedUsers")
true false true ❌ false ✅(skip item, it includes "receiver")
true true true ❌ true ✅(this is the only case where you want to keep the item, when it doesn't include either string)
about 4 years ago · Juan Pablo Isaza Report

0

You can combine Array.prototype.filter() with Array.prototype.includes():

const arr = [{ _id: 'receiver' },{ _id: 'blockedUsers' },{ _id: 'MjIzx3XA1mpcuzgDVZj', createdAt: 1631349363111, text: 'Ok', user: { name: 'Nikki' } },{ _id: 'M6fBsPludfYVjJXKYvwgxHRacYw1' },{ _id: ' MjG3hFAgcNweJWh9SF7', createdAt: 1631300277391, text: 'Again', user: { name: 'Chris', _id: 'tFhmw5oQoPhk8nF2sx5rE5BFqw93' }}]

const result = arr.filter(({ _id }) => !['receiver', 'blockedUsers'].includes(_id))

console.log(result)

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!