gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:44:13 25 4
gpt4 key购买 nike

我正在尝试按最低数量 amoutinINR 键对医院数组进行排序,但我坚持对医院的深层嵌套 Object array 进行排序。任何伙伴都有知识 如何在没有任何第三方库(如 lodash 或 underscore)的情况下进行排序。

这是我的 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
}]
}]

我想按最低的 amoutinINR 值对医院进行排序,并返回相同的 JSON 对象,其中包含已排序的医院。拜托,任何人都可以帮助我如何通过优化代码来实现吗?

最佳答案

基本上,您可以使用 Array#sortresult 中的每个数组进行排序并用 Array#forEach 迭代,因为您的 map 不返回任何内容。

result.forEach(r => r.procedure.forEach(p => p.hospital.sort((a, b) => a.amountinINR - b.amountinINR)));

关于javascript - 如何按值对深层嵌套对象进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41547339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com