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.
JSON is unordered, and the browser will automatically sort according to key, so sorting is useless.
JSON
,
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?
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
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?