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

102
Views
Return a message if nothing is found after filtering an array in JavaScript

I'm getting an array in React/Next, but before mapping through it I'm running a few filters and sorting it. I'd like to display a simple message if nothing gets found after the filters run. It should display a span with a simple text string (jsx).

The code is pretty simple:

{ArrayObj
            .filter(
              (item) =>
                // conditions here
            )
            .sort((a, b) => (a.condition > b.condition ? 1 : -1))
            .map((item) => (
              <li key={item._id}>
                // contents of each iteration here
              </li>
))}

I'm guessing it should go just right before the .map but I'm stuck at this.

The full code is as follows:

{yachtListings
            .filter(
              (yacht) =>
                yacht.buildYear >= searchYear &&
                yacht.price <= searchPrice &&
                yacht.length <= searchLength &&
                yacht.type === searchType
            )
            .filter((yacht) =>
              filterKeyword == ''
                ? yacht
                : yacht.modelName
                    .toLowerCase()
                    .includes(filterKeyword.toLowerCase())
            )
            .sort((a, b) => (a[sortOrder] > b[sortOrder] ? 1 : -1))
            .map((yacht) => (
              <li key={yacht._id}>
                <SecondHandCard />  // card components with props
              </li>
            ))}

You can see a live demo here. I'm a front-end guy taking my skills to the back-end more and more.

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

0

You could filter the array first and then conditionally show jsx depending on the length of that.

let filteredArray = ArrayObj.filter((item) => {...}).sort(...);
{filteredArray.length == 0 ? <span>Message</span> : filteredArray.map(...)}
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!