如何按值对深层嵌套对象进行排序?

时间:2017-01-09 11:44:46

标签: javascript json sorting arrayobject

我正在尝试按最低金额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对象。请问,任何人都可以通过优化的代码帮助我实现目标吗?

3 个答案:

答案 0 :(得分:1)

基本上,cou可以使用Array#sort @IBAction func btnLogin(_ sender: UIButton) { if (self.IDtext.text?.isEmpty)! { // Show Error } else if (self.IDtext.text? == "12456"){ let storyboard = UIStoryboard(name: storyboard, bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: "nextController") self.navigationController?.pushViewController(viewController, animated: true) } } 内的每个数组进行排序,并使用Array#forEach进行迭代,因为您的地图不返回任何内容。

result

答案 1 :(得分:1)

基本上与Nina Scholz的答案相同,但使用较旧的方法。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我知道它的回答可能是其他人认为它有用一个班轮。 @Nina Scholz

&#13;
&#13;
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);
&#13;
&#13;
&#13;