var arr = {
'2021-07-20-21:10': {
sends: 1,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 0,
appErrors: 0,
responseTimeAvg: 172,
responseTimeMax: 172,
when: '21:10',
hostCount: 1,
},
'2021-07-20-21:22': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:22',
hostCount: 1,
},
'2021-07-20-21:13': {
sends: 2,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 89,
responseTimeMax: 177,
when: '21:13',
hostCount: 2,
},
'2021-07-20-21:14': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:14',
hostCount: 1,
}}
并且必须根据键按desc顺序进行排序
var arr = {
'2021-07-20-21:22': {
sends: 1,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 0,
appErrors: 0,
responseTimeAvg: 172,
responseTimeMax: 172,
when: '21:22',
hostCount: 1,
},
'2021-07-20-21:14': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:14',
hostCount: 1,
},
'2021-07-20-21:13': {
sends: 2,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 89,
responseTimeMax: 177,
when: '21:13',
hostCount: 2,
},
'2021-07-20-21:10': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:10',
hostCount: 1,
}}
1条答案
按热度按时间9gm1akwq1#
你可以使用Object.keys()函数提取数组的“keys”,然后使用sort函数对数组的键进行排序,并生成一个新的对象,迭代每个键...代码如下(保留 arr 作为数组的名称):
......当然,这可以大大缩短,例如:
编辑:我看到你需要按 * 降序 * 排序数组。这可以使用自定义排序函数或使用函数reverse()来完成。只需更正排序步骤: