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)
What am I missing in this one, Any help is appreciated