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,
}
]