gpt4 book ai didi

javascript - 按动态键对嵌套对象数组进行排序

转载 作者:行者123 更新时间:2023-12-03 11:19:11 25 4
gpt4 key购买 nike

我相信我对这个问题的措辞是正确的......我有一个这样的数据集......

[
{
"Fri Aug 03 2018 00:00:00 GMT-0500": {
"bill": "Test 2",
"account": "defaultAccount",
"amount": 40,
"category": "defaultCategory",
"description": "231"
}
},
{
"Fri Aug 17 2018 00:00:00 GMT-0500": {
"bill": "Test 2",
"account": "defaultAccount",
"amount": 40,
"category": "defaultCategory",
"description": "231"
}
},
{
"Wed Aug 01 2018 00:00:00 GMT-0500": {
"bill": "test",
"account": "create-account",
"amount": 40,
"category": "mortgage",
"description": "23"
}
}, ....
]

我正在努力根据对象的动态日期对数组进行排序。在过去的几个小时里,我一直在研究这个关于堆栈溢出的问题的每个版本,但我没有提出解决方案……非常感谢任何建议!

最佳答案

您需要获取每个外部对象的键并获取一个日期对象。然后取delta进行排序。

var array = [{ "Fri Aug 03 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Fri Aug 17 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Wed Aug 01 2018 00:00:00 GMT-0500": { bill: "test", account: "create-account", amount: 40, category: "mortgage", description: "23" } }];

array.sort((a, b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 按动态键对嵌套对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51735283/

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