gpt4 book ai didi

javascript - 如何按属性值对 JavaScript 中的对象数组进行排序?

转载 作者:行者123 更新时间:2023-12-01 01:15:24 26 4
gpt4 key购买 nike

有一个对象数组,例如:

我想按 itemsArray 的 sortOrder 对 itemsArray 进行排序。

这是项目的排序顺序 (itemsArray['item=a37d2733-f710-4e47-8573-6aab11f6d020'].sortOrder)

{
"Items": [
{
"sectionId": "section=38",
"itemsArray": {
"item=a37d2733-f710-4e47-8573-6aab11f6d020": {
"itemObject": {
"description": "Really good food.",
"price": 1000,
"name": "Cappuccino",
"SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d020",
},
"sortOrder": 3
},
"item=bd844a27-0a00-445d-a40a-785cf16fwine": {
"itemObject": {
"description": "Really good wine.",
"price": 3000,
"name": "Red Wine",
"SKey": "item=bd844a27-0a00-445d-a40a-785cf16fwine",
},
"sortOrder": 1
},
"item=a37d2733-f710-4e47-8573-6aab11f6d016": {
"itemObject": {
"description": "Really good drink.",
"price": 1000,
"name": "Orange Juice",
"SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d016",
},
"sortOrder": 2
},
"item=bd844a27-0a00-445d-a40a-785cf16f4950": {
"itemObject": {
"description": "Really good drink.",
"price": 1000,
"name": "Coca-Cola - sanjy",
"SKey": "item=bd844a27-0a00-445d-a40a-785cf16f4950",
},
"sortOrder": 0
}
},
"description": "Non-alcoholic beverages.",
"sectionName": "Beverages",
}
]
}

排序后的数组将是这样的:

{
"Items": [
{
"sectionId": "section=38",
"itemsArray": {
"item=bd844a27-0a00-445d-a40a-785cf16f4950": {
"itemObject": {
"description": "Really good drink.",
"price": 1000,
"name": "Coca-Cola - sanjy",
"SKey": "item=bd844a27-0a00-445d-a40a-785cf16f4950",
},
"sortOrder": 0
},
"item=bd844a27-0a00-445d-a40a-785cf16fwine": {
"itemObject": {
"description": "Really good wine.",
"price": 3000,
"name": "Red Wine",
"SKey": "item=bd844a27-0a00-445d-a40a-785cf16fwine",
},
"sortOrder": 1
},
"item=a37d2733-f710-4e47-8573-6aab11f6d016": {
"itemObject": {
"description": "Really good drink.",
"price": 1000,
"name": "Orange Juice",
"SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d016",
},
"sortOrder": 2
},
"item=a37d2733-f710-4e47-8573-6aab11f6d020": {
"itemObject": {
"description": "Really good food.",
"price": 1000,
"name": "Cappuccino",
"SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d020",
},
"sortOrder": 3
}
},
"description": "Non-alcoholic beverages.",
"sectionName": "Beverages",

}
]
}

我已经在这里尝试使用 array.sort 方法,但没有成功。因此,如果有人有解决方案,那么它会很有帮助。

最佳答案

我会考虑将 JSON 重构为可排序,例如:

{
"Items": [
{
"sectionId": "section=38",
"sectionName": "Beverages",
"description": "Non-alcoholic beverages.",
"itemsArray": [
{
"id": "item=a37d2733-f710-4e47-8573-6aab11f6d020",
"description": "Really good food.",
"price": 1000,
"name": "Cappuccino",
"sortOrder": 3
},
{
"id": "item=bd844a27-0a00-445d-a40a-785cf16fwine",
"description": "Really good wine.",
"price": 3000,
"name": "Red Wine",
"sortOrder": 1
},
{
"id": "item=a37d2733-f710-4e47-8573-6aab11f6d016",
"description": "Really good drink.",
"price": 1000,
"name": "Orange Juice",
"sortOrder": 2
},
{
"id": "item=bd844a27-0a00-445d-a40a-785cf16f4950",
"description": "Really good drink.",
"price": 1000,
"name": "Coca-Cola - sanjy",
"sortOrder": 0
}
]
}
]
}

可以使用 sort 升序排序:Items[0].itemsArray.sort((a, b) => a.sortOrder - b.sortOrder)

更改包括使 itemsArray 成为一个数组,并将项目 id 移动到 itemsArray 的每个项目中其自己的属性中。

<小时/>

我不建议按对象排序,因为对象键的排序顺序历来无法保证。但是以下函数可以按照您当前的结构对 json 对象进行排序,改编自 this Stack Overflow answer :

function sortObject(o) {
var sorted = {},
key, a = [];

for (key in o) {
if (o.hasOwnProperty(key)) {
a[o[key].sortOrder] = key;
}
}

for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}

json.Items[0].itemsArray = sortObject(json.Items[0].itemsArray);
<小时/>

请注意,这假设您的 json 对象具有来自 0...nsortOrder 属性,因为它通过添加到 array[sortOrder]< 来填充数组 而不是 array.push,然后迭代该数组以填充结果对象。

关于javascript - 如何按属性值对 JavaScript 中的对象数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807409/

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