I need assistance sorting an array of hospitals based on the lowest value of the amountinINR
key, especially when dealing with deeply nested arrays of hospital objects. Does anyone know how to achieve this without using third-party libraries like lodash or underscore?
Below is a snippet of my JSON
object:
// My first attempt
for (let i = 0; i < result.length; i++) {
for (let j = 0; j < result[i].procedure.length; j++) {
for (let k = 0; k < result[i].procedure[j].hospital.length; k++) {
if (k === 0 || k === 1 || k === 2) {
result[i].procedure[j].hospital[k].amountinDoller = `<b> ${hospitalDol.amountinDoller || 'N/A'} </b>`;
result[i].procedure[j].hospital[k].amountinINR = `<b>${hospitalInr[k].amountinINR || 'N/A'} </b>`
}
}
}
}
---------------------------------------------------------------------------------------
// here i am using map() native method of javascript
** my Second Attempt**
result.map((category,index) => {
return category.procedure.map((procedures,index) => {
return procedures.hospital.sort((a,b) => { return a.amountinINR - b.amountinINR});
})
});
// JSON Object
let result = [{
"_id": "58711a64546a7b5bf2d07b4d",
"procedure": [...] // Truncated for brevity
"category": [{
"_id": "58711a64546a7b5bf2d07b4d",
"name": "Cancer Surgeries",
"status": true,
"createdAt": "2017-01-07T16:42:12.696Z",
"__v": 0
}]
}]
I'm looking for a way to sort hospitals by the lowest amountinINR
value and return the same JSON
object with the sorted hospitals. Any optimized code suggestions would be greatly appreciated.