Я пытаюсь отсортировать массив больниц по ключу amountinINR с наименьшим количеством, но я застрял, чтобы отсортировать Object array больницу с глубоким вложением. Любой приятель знает как сортировать без какой-либо сторонней библиотеки, например, lodash или подчеркивания.

Вот мой JSON объект. и я пытаюсь решить, как это

   // 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": [{
                    "_id": "58711aa8546a7b5bf2d07b4e",
                    "hospital": [{
                        "_id": "5872209951716427b860351d",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 310020,
                        "amountinDoller": 7000,
                        "hospitalId": "5870eb66423c3231d4a19eb6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587237fa51716427b860357a",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 385440,
                        "amountinDoller": 7300,
                        "hospitalId": "5870e89a3fd9ac2e3aaee967",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58728cb751716427b8603604",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 488500,
                        "amountinDoller": 7400,
                        "hospitalId": "5870ec605c3f3f3806171ed6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872450c51716427b86035e0",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 351780,
                        "amountinDoller": 8200,
                        "hospitalId": "5870eefd8b40563f0e15115e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872125e51716427b86034f6",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 368511,
                        "amountinDoller": 8590,
                        "hospitalId": "5870f66c5a8cbc4ac19afa7e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587125b451716427b8603476",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 480480,
                        "amountinDoller": 9100,
                        "hospitalId": "5870f2afb4255844bf0c2a10",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58713aa151716427b860349e",
                        "procedureId": "58711aa8546a7b5bf2d07b4e",
                        "amountinINR": 407550,
                        "amountinDoller": 9500,
                        "hospitalId": "5870edf92f3a483c2098b714",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }],
                    "procedures": {
                        "_id": "58711aa8546a7b5bf2d07b4e",
                        "name": "Breast Conservative surgery",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "status": true,
                        "createdAt": "2017-01-07T16:43:20.888Z",
                        "__v": 0
                    }
                }, {
                    "_id": "58711b8d546a7b5bf2d07b52",
                    "hospital": [{
                        "_id": "58728d4551716427b8603608",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 198000,
                        "amountinDoller": 3000,
                        "hospitalId": "5870ec605c3f3f3806171ed6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872213651716427b8603521",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 141800,
                        "amountinDoller": 3300,
                        "hospitalId": "5870eb66423c3231d4a19eb6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587212fb51716427b86034fa",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 143715,
                        "amountinDoller": 3350,
                        "hospitalId": "5870f66c5a8cbc4ac19afa7e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5871268551716427b860347a",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 174240,
                        "amountinDoller": 3390,
                        "hospitalId": "5870f2afb4255844bf0c2a10",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872388551716427b860357e",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 179520,
                        "amountinDoller": 3400,
                        "hospitalId": "5870e89a3fd9ac2e3aaee967",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587245a951716427b86035e7",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 150150,
                        "amountinDoller": 3500,
                        "hospitalId": "5870eefd8b40563f0e15115e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58713b3c51716427b86034a2",
                        "procedureId": "58711b8d546a7b5bf2d07b52",
                        "amountinINR": 163020,
                        "amountinDoller": 3800,
                        "hospitalId": "5870edf92f3a483c2098b714",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }],
                    "procedures": {
                        "_id": "58711b8d546a7b5bf2d07b52",
                        "name": "Hysterectomy + Laparotomy",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "status": true,
                        "createdAt": "2017-01-07T16:47:09.982Z",
                        "__v": 0
                    }
                }, {
                    "_id": "58711b4a546a7b5bf2d07b51",
                    "hospital": [{
                        "_id": "58728d2651716427b8603607",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 349900,
                        "amountinDoller": 5300,
                        "hospitalId": "5870ec605c3f3f3806171ed6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58713b1b51716427b86034a1",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 235950,
                        "amountinDoller": 5500,
                        "hospitalId": "5870edf92f3a483c2098b714",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872210451716427b8603520",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 240000,
                        "amountinDoller": 5600,
                        "hospitalId": "5870eb66423c3231d4a19eb6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872386551716427b860357d",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 311520,
                        "amountinDoller": 5900,
                        "hospitalId": "5870e89a3fd9ac2e3aaee967",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872457f51716427b86035e6",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 253110,
                        "amountinDoller": 5900,
                        "hospitalId": "5870eefd8b40563f0e15115e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5871264a51716427b8603479",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 327360,
                        "amountinDoller": 6200,
                        "hospitalId": "5870f2afb4255844bf0c2a10",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587212d051716427b86034f9",
                        "procedureId": "58711b4a546a7b5bf2d07b51",
                        "amountinINR": 277563,
                        "amountinDoller": 6470,
                        "hospitalId": "5870f66c5a8cbc4ac19afa7e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }],
                    "procedures": {
                        "_id": "58711b4a546a7b5bf2d07b51",
                        "name": "Modified Neck Dissection- MND",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "status": true,
                        "createdAt": "2017-01-07T16:46:02.044Z",
                        "__v": 0
                    }
                }, {
                    "_id": "58711b05546a7b5bf2d07b4f",
                    "hospital": [{
                        "_id": "58713ac151716427b860349f",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 235950,
                        "amountinDoller": 5500,
                        "hospitalId": "5870edf92f3a483c2098b714",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872381951716427b860357b",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 438240,
                        "amountinDoller": 8300,
                        "hospitalId": "5870e89a3fd9ac2e3aaee967",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587220f051716427b860351f",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 368900,
                        "amountinDoller": 8600,
                        "hospitalId": "5870eb66423c3231d4a19eb6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58728cde51716427b8603605",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 587500,
                        "amountinDoller": 8900,
                        "hospitalId": "5870ec605c3f3f3806171ed6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587125d951716427b8603477",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 475200,
                        "amountinDoller": 9000,
                        "hospitalId": "5870f2afb4255844bf0c2a10",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872127d51716427b86034f7",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 398112,
                        "amountinDoller": 9280,
                        "hospitalId": "5870f66c5a8cbc4ac19afa7e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872452951716427b86035e2",
                        "procedureId": "58711b05546a7b5bf2d07b4f",
                        "amountinINR": 407550,
                        "amountinDoller": 9500,
                        "hospitalId": "5870eefd8b40563f0e15115e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }],
                    "procedures": {
                        "_id": "58711b05546a7b5bf2d07b4f",
                        "name": "Prophylactic Mastectomy",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "status": true,
                        "createdAt": "2017-01-07T16:44:53.289Z",
                        "__v": 0
                    }
                }, {
                    "_id": "58711b36546a7b5bf2d07b50",
                    "hospital": [{
                        "_id": "587220b351716427b860351e",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 257500,
                        "amountinDoller": 6000,
                        "hospitalId": "5870eb66423c3231d4a19eb6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872455951716427b86035e3",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 270270,
                        "amountinDoller": 6300,
                        "hospitalId": "5870eefd8b40563f0e15115e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58713af551716427b86034a0",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 287430,
                        "amountinDoller": 6700,
                        "hospitalId": "5870edf92f3a483c2098b714",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5872384051716427b860357c",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 369600,
                        "amountinDoller": 7000,
                        "hospitalId": "5870e89a3fd9ac2e3aaee967",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "587212b551716427b86034f8",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 304590,
                        "amountinDoller": 7100,
                        "hospitalId": "5870f66c5a8cbc4ac19afa7e",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "58728cfe51716427b8603606",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 481900,
                        "amountinDoller": 7300,
                        "hospitalId": "5870ec605c3f3f3806171ed6",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }, {
                        "_id": "5871261f51716427b8603478",
                        "procedureId": "58711b36546a7b5bf2d07b50",
                        "amountinINR": 396000,
                        "amountinDoller": 7500,
                        "hospitalId": "5870f2afb4255844bf0c2a10",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "__v": 0
                    }],
                    "procedures": {
                        "_id": "58711b36546a7b5bf2d07b50",
                        "name": "Total Thyroidectomy",
                        "categoryId": "58711a64546a7b5bf2d07b4d",
                        "status": true,
                        "createdAt": "2017-01-07T16:45:42.094Z",
                        "__v": 0
                    }
                }],
                "category": [{
                    "_id": "58711a64546a7b5bf2d07b4d",
                    "name": "Cancer Surgeries",
                    "status": true,
                    "createdAt": "2017-01-07T16:42:12.696Z",
                    "__v": 0
                }]
            }]

Я хочу отсортировать больницу по наименьшему значению amountinINR и вернуть тот же объект JSON с отсортированной больницей внутри. Пожалуйста, кто-нибудь может мне помочь, как я могу добиться с помощью оптимизированного кода?

2
Manjeet Thakur 9 Янв 2017 в 14:44

3 ответа

Лучший ответ

По сути, cou может отсортировать каждый массив внутри result с помощью Array#sort и выполните итерацию с Array#forEach, потому что ваша карта ничего не возвращает.

result.forEach(r => r.procedure.forEach(p => p.hospital.sort((a, b) => a.amountinINR - b.amountinINR)));
1
Nina Scholz 9 Янв 2017 в 14:33

В основном тот же ответ, что и Nina Scholz s, но с использованием более старого метода.

var results = [{
  "_id": "58711a64546a7b5bf2d07b4d",
  "procedure": [{
    "_id": "58711aa8546a7b5bf2d07b4e",
    "hospital": [{
      "_id": "5872209951716427b860351d",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 310020,
      "amountinDoller": 7000,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587237fa51716427b860357a",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 385440,
      "amountinDoller": 7300,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58728cb751716427b8603604",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 488500,
      "amountinDoller": 7400,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872450c51716427b86035e0",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 351780,
      "amountinDoller": 8200,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872125e51716427b86034f6",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 368511,
      "amountinDoller": 8590,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587125b451716427b8603476",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 480480,
      "amountinDoller": 9100,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713aa151716427b860349e",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 407550,
      "amountinDoller": 9500,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711aa8546a7b5bf2d07b4e",
      "name": "Breast Conservative surgery",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:43:20.888Z",
      "__v": 0
    }
  }, {
    "_id": "58711b8d546a7b5bf2d07b52",
    "hospital": [{
      "_id": "58728d4551716427b8603608",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 198000,
      "amountinDoller": 3000,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872213651716427b8603521",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 141800,
      "amountinDoller": 3300,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587212fb51716427b86034fa",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 143715,
      "amountinDoller": 3350,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5871268551716427b860347a",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 174240,
      "amountinDoller": 3390,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872388551716427b860357e",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 179520,
      "amountinDoller": 3400,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587245a951716427b86035e7",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 150150,
      "amountinDoller": 3500,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713b3c51716427b86034a2",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 163020,
      "amountinDoller": 3800,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b8d546a7b5bf2d07b52",
      "name": "Hysterectomy + Laparotomy",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:47:09.982Z",
      "__v": 0
    }
  }, {
    "_id": "58711b4a546a7b5bf2d07b51",
    "hospital": [{
      "_id": "58728d2651716427b8603607",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 349900,
      "amountinDoller": 5300,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713b1b51716427b86034a1",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 235950,
      "amountinDoller": 5500,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872210451716427b8603520",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 240000,
      "amountinDoller": 5600,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872386551716427b860357d",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 311520,
      "amountinDoller": 5900,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872457f51716427b86035e6",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 253110,
      "amountinDoller": 5900,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5871264a51716427b8603479",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 327360,
      "amountinDoller": 6200,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587212d051716427b86034f9",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 277563,
      "amountinDoller": 6470,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b4a546a7b5bf2d07b51",
      "name": "Modified Neck Dissection- MND",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:46:02.044Z",
      "__v": 0
    }
  }, {
    "_id": "58711b05546a7b5bf2d07b4f",
    "hospital": [{
      "_id": "58713ac151716427b860349f",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 235950,
      "amountinDoller": 5500,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872381951716427b860357b",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 438240,
      "amountinDoller": 8300,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587220f051716427b860351f",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 368900,
      "amountinDoller": 8600,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58728cde51716427b8603605",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 587500,
      "amountinDoller": 8900,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587125d951716427b8603477",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 475200,
      "amountinDoller": 9000,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872127d51716427b86034f7",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 398112,
      "amountinDoller": 9280,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872452951716427b86035e2",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 407550,
      "amountinDoller": 9500,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b05546a7b5bf2d07b4f",
      "name": "Prophylactic Mastectomy",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:44:53.289Z",
      "__v": 0
    }
  }, {
    "_id": "58711b36546a7b5bf2d07b50",
    "hospital": [{
      "_id": "587220b351716427b860351e",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 257500,
      "amountinDoller": 6000,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872455951716427b86035e3",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 270270,
      "amountinDoller": 6300,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713af551716427b86034a0",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 287430,
      "amountinDoller": 6700,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872384051716427b860357c",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 369600,
      "amountinDoller": 7000,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587212b551716427b86034f8",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 304590,
      "amountinDoller": 7100,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58728cfe51716427b8603606",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 481900,
      "amountinDoller": 7300,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5871261f51716427b8603478",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 396000,
      "amountinDoller": 7500,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b36546a7b5bf2d07b50",
      "name": "Total Thyroidectomy",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:45:42.094Z",
      "__v": 0
    }
  }],
  "category": [{
    "_id": "58711a64546a7b5bf2d07b4d",
    "name": "Cancer Surgeries",
    "status": true,
    "createdAt": "2017-01-07T16:42:12.696Z",
    "__v": 0
  }]
}];
//Loop throught results
for (var resultIndex = 0; resultIndex < results.length; resultIndex++) {
  var result = results[resultIndex];
  for (var procedureIndex = 0; procedureIndex < result.procedure.length; procedureIndex++) {
    var procedure = result.procedure[procedureIndex];
    //foreach result, modify "procedure"
    procedure.hospital = procedure.hospital
      .sort(function(a, b) {
        //Sort list to "amountinINR" DESCENDING
        return a.amountinINR - b.amountinINR;
        //Sort list to "amountinINR" ASCENDING
        //return b.amountinINR - a.amountinINR;
      });
  }
}
//Log results
console.log(results);
1
Emil S. Jørgensen 9 Янв 2017 в 12:12

Я знаю, что ответом может быть кто-то еще считает полезным один вкладыш . @ Нина Шольц

let result = [{
  "_id": "58711a64546a7b5bf2d07b4d",
  "procedure": [{
    "_id": "58711aa8546a7b5bf2d07b4e",
    "hospital": [{
      "_id": "5872209951716427b860351d",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 310020,
      "amountinDoller": 7000,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587237fa51716427b860357a",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 385440,
      "amountinDoller": 7300,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58728cb751716427b8603604",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 488500,
      "amountinDoller": 7400,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872450c51716427b86035e0",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 351780,
      "amountinDoller": 8200,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872125e51716427b86034f6",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 368511,
      "amountinDoller": 8590,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587125b451716427b8603476",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 480480,
      "amountinDoller": 9100,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713aa151716427b860349e",
      "procedureId": "58711aa8546a7b5bf2d07b4e",
      "amountinINR": 407550,
      "amountinDoller": 9500,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711aa8546a7b5bf2d07b4e",
      "name": "Breast Conservative surgery",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:43:20.888Z",
      "__v": 0
    }
  }, {
    "_id": "58711b8d546a7b5bf2d07b52",
    "hospital": [{
      "_id": "58728d4551716427b8603608",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 198000,
      "amountinDoller": 3000,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872213651716427b8603521",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 141800,
      "amountinDoller": 3300,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587212fb51716427b86034fa",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 143715,
      "amountinDoller": 3350,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5871268551716427b860347a",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 174240,
      "amountinDoller": 3390,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872388551716427b860357e",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 179520,
      "amountinDoller": 3400,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587245a951716427b86035e7",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 150150,
      "amountinDoller": 3500,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713b3c51716427b86034a2",
      "procedureId": "58711b8d546a7b5bf2d07b52",
      "amountinINR": 163020,
      "amountinDoller": 3800,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b8d546a7b5bf2d07b52",
      "name": "Hysterectomy + Laparotomy",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:47:09.982Z",
      "__v": 0
    }
  }, {
    "_id": "58711b4a546a7b5bf2d07b51",
    "hospital": [{
      "_id": "58728d2651716427b8603607",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 349900,
      "amountinDoller": 5300,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713b1b51716427b86034a1",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 235950,
      "amountinDoller": 5500,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872210451716427b8603520",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 240000,
      "amountinDoller": 5600,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872386551716427b860357d",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 311520,
      "amountinDoller": 5900,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872457f51716427b86035e6",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 253110,
      "amountinDoller": 5900,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5871264a51716427b8603479",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 327360,
      "amountinDoller": 6200,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587212d051716427b86034f9",
      "procedureId": "58711b4a546a7b5bf2d07b51",
      "amountinINR": 277563,
      "amountinDoller": 6470,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b4a546a7b5bf2d07b51",
      "name": "Modified Neck Dissection- MND",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:46:02.044Z",
      "__v": 0
    }
  }, {
    "_id": "58711b05546a7b5bf2d07b4f",
    "hospital": [{
      "_id": "58713ac151716427b860349f",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 235950,
      "amountinDoller": 5500,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872381951716427b860357b",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 438240,
      "amountinDoller": 8300,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587220f051716427b860351f",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 368900,
      "amountinDoller": 8600,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58728cde51716427b8603605",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 587500,
      "amountinDoller": 8900,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587125d951716427b8603477",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 475200,
      "amountinDoller": 9000,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872127d51716427b86034f7",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 398112,
      "amountinDoller": 9280,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872452951716427b86035e2",
      "procedureId": "58711b05546a7b5bf2d07b4f",
      "amountinINR": 407550,
      "amountinDoller": 9500,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b05546a7b5bf2d07b4f",
      "name": "Prophylactic Mastectomy",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:44:53.289Z",
      "__v": 0
    }
  }, {
    "_id": "58711b36546a7b5bf2d07b50",
    "hospital": [{
      "_id": "587220b351716427b860351e",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 257500,
      "amountinDoller": 6000,
      "hospitalId": "5870eb66423c3231d4a19eb6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872455951716427b86035e3",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 270270,
      "amountinDoller": 6300,
      "hospitalId": "5870eefd8b40563f0e15115e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58713af551716427b86034a0",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 287430,
      "amountinDoller": 6700,
      "hospitalId": "5870edf92f3a483c2098b714",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5872384051716427b860357c",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 369600,
      "amountinDoller": 7000,
      "hospitalId": "5870e89a3fd9ac2e3aaee967",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "587212b551716427b86034f8",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 304590,
      "amountinDoller": 7100,
      "hospitalId": "5870f66c5a8cbc4ac19afa7e",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "58728cfe51716427b8603606",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 481900,
      "amountinDoller": 7300,
      "hospitalId": "5870ec605c3f3f3806171ed6",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }, {
      "_id": "5871261f51716427b8603478",
      "procedureId": "58711b36546a7b5bf2d07b50",
      "amountinINR": 396000,
      "amountinDoller": 7500,
      "hospitalId": "5870f2afb4255844bf0c2a10",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "__v": 0
    }],
    "procedures": {
      "_id": "58711b36546a7b5bf2d07b50",
      "name": "Total Thyroidectomy",
      "categoryId": "58711a64546a7b5bf2d07b4d",
      "status": true,
      "createdAt": "2017-01-07T16:45:42.094Z",
      "__v": 0
    }
  }],
  "category": [{
    "_id": "58711a64546a7b5bf2d07b4d",
    "name": "Cancer Surgeries",
    "status": true,
    "createdAt": "2017-01-07T16:42:12.696Z",
    "__v": 0
  }]
}];
// sorting
result.forEach(p => p.procedure.forEach(x => x.hospital.sort(function(a, b) {
  return (a.amountinINR - b.amountinINR);
})));
console.log(result);
1
MMK 9 Янв 2017 в 13:37