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

230
Views
Sorting array based on property existence

I have an array, i need to sort the array based on one property if that exists.

const arr = [{
    "item_name": "Cake",
    "quantity": 1,
  },
  {
    "item_name": "Choclate",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 3,

  }
]

let output = arr.sort((a, b) => {
  if (a.out_of_stock_detail) {
    return 1
  } else {
    return -1
  }
})

console.log(output)

Expected output what I am looking out to get.

const result = [{
    "item_name": "Choclate",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 3,

  },
  {
    "item_name": "Cake",
    "quantity": 1,
  }
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here's an option. Create a numeric value based on the existence of the property then return the comparison. I also added a second sort so highest quantities are first.

const arr = [{
    "item_name": "Cake",
    "quantity": 1,
  },
  {
    "item_name": "Choclate",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 3,
  },
  {
    "item_name": "Vanilla",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 9,
  }
]

let output = arr.sort((a, b) => {
  let aa = a.hasOwnProperty('out_of_stock_detail') ? 1 : -1
  let bb = b.hasOwnProperty('out_of_stock_detail') ? 1 : -1
  return bb - aa;
}).sort((a, b) => +b.quantity - +a.quantity)

console.log(output)

about 4 years ago · Juan Pablo Isaza Report

0

Just a slightly other approach by taking a boolean and taking the delta of it.

BTW, Array#sort sorts in situ (mutating the array).

const
    array = [{ item_name: "Cake", quantity: 1 }, { item_name: "Choclate", out_of_stock_detail: { out_of_stock_quantity: 1 }, quantity: 3 }];

array.sort((a, b) => 
    ('out_of_stock_detail' in b) - ('out_of_stock_detail' in a)
);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Didn't you just mix up the 1 and -1?

const arr =  [
        {
            "item_name": "Cake",
            "quantity": 1,
        },
        {
            "item_name": "Choclate",
            "out_of_stock_detail": {
                "out_of_stock_quantity": 1
            },
            "quantity": 3,
    
        }
    ]

let output = arr.sort((a,b) => {
   if(a.out_of_stock_detail){
    return -1 
  }else {
    return 1
  }
})

console.log(output)

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!