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

100
Views
Order an array of objects by whether property exists

I have an array of teachers, where each one have a property user with image property, that can contain a string or null value. How can i order the array, considering the user.image property?

Here is an example of structure.

const teachers = [
    {
        id: 1,
        user: {
            id: 10,
            image: "image.jgp"
        }
    },
    {
        id: 3,
        user: {
            id: 30,
            image: null
        }
    },
    {
        id: 2,
        user: {
            id: 20,
            image: "image.jgp"
        }
    },
    {
        id: 4,
        user: {
            id: 40,
            image: null
        }
    },
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use Array.sort and check whether the image property is null:

const teachers=[{id:1,user:{id:10,image:"image.jgp"}},{id:3,user:{id:30,image:null}},{id:2,user:{id:20,image:"image.jgp"}},{id:4,user:{id:40,image:null}}];

const sorted = teachers.sort((a, b) => b.user.image === null ? -1 : 1)

console.log(sorted)

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!