javascript - How to sort this object
巴扎黑
巴扎黑 2017-06-30 09:59:25
0
5
768
var obj = {
    "10": 20.1,
    "11": 16,
    "12": 12.7,
    "01": 0,
    "02": 0,
    "03": 0,
    "04": 0,
    "05": 0,
    "06": 0,
    "07": 0,
    "08": 27.6,
    "09": 24.3
};

Sort by key value.

巴扎黑
巴扎黑

reply all(5)
伊谢尔伦

JSON is unordered, and the browser will automatically sort according to key, so sorting is useless.

,

学霸

It is recommended to convert to array first, then sort, and then convert to object

学霸
    var obj = {
      '10': 20.1,
      '11': 16,
      '12': 12.7,
      '01': 0,
      '02': 0,
      '03': 0,
      '04': 0,
      '05': 0,
      '06': 0,
      '07': 0,
      '08': 27.6,
      '09': 24.3
    }
    console.log(Object.keys(obj).sort().reduce((a, b) => (a[b] = obj[b], a), {}))
扔个三星炸死你
var arr = []
for (const key in obj) {
  arr[key] = obj[key]
}

This can achieve your needs

If the middle is not continuous, you need to filter it again later

阿神

Why do objects need to be sorted? Can’t we get the setting value directly through the key value?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template