js 对象 根据 key 排序

 

const unordered = { 'b': 'foo', 'c': 'bar', 'a': 'baz' }; console.log(JSON.stringify(unordered)); // → '{"b":"foo","c":"bar","a":"baz"}' const ordered = {}; Object.keys(unordered).sort().forEach(function(key) { ordered[key] = unordered[key]; }); console.log(JSON.stringify(ordered)); // → '{"a":"baz","b":"foo","c":"bar"}'

来源:https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key

转载于:https://www.cnblogs.com/shaoyang0123/p/10830554.html

你可能感兴趣的:(js 对象 根据 key 排序)