如何使用angular按键降序对JSON数组排序

vs3odd8k  于 2023-04-08  发布在  Angular
关注(0)|答案(1)|浏览(139)
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,
      }}
9gm1akwq

9gm1akwq1#

你可以使用Object.keys()函数提取数组的“keys”,然后使用sort函数对数组的键进行排序,并生成一个新的对象,迭代每个键...代码如下(保留 arr 作为数组的名称):

// Get the keys of your "array"
let arrayOfKeys = Object.keys(arr)

// We sort it out...
let sortedArray = arrayOfKeys.sort()

// We will create the "final" array
let resultingArray = {}

// Now we iterate over each element, in order:
sortedArray.forEach((element) => {
    resultingArray[element] = arr[element]
})

......当然,这可以大大缩短,例如:

var resultingArr = {}
Object.keys(arr).sort().forEach(e -> { resultingArr[e] = arr[e] })

编辑:我看到你需要按 * 降序 * 排序数组。这可以使用自定义排序函数或使用函数reverse()来完成。只需更正排序步骤:

// We sort it out...
let sortedArray = arrayOfKeys.sort().reverse()

相关问题