var myObj = {'b': 'asdsadfd','c': 'masdasaf','a': 'dsfdsfsdf'},keys = [],k, i, len;
for (k in myObj) {if (myObj.hasOwnProperty(k)) {keys.push(k);}}
keys.sort();
len = keys.length;
for (i = 0; i < len; i++) {k = keys[i];console.log(k + ':' + myObj[k]);}
some_map = { 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' }
// perform a function in order of ascending key_(some_map).keys().sort().each(function (key) {var value = some_map[key];// do something});
// or alternatively to build a sorted listsorted_list = _(some_map).keys().sort().map(function (key) {var value = some_map[key];// return something that shall become an item in the sorted list}).value();
/*** Returns a copy of an object, which is ordered by the keys of the original object.** @param {Object} object - The original object.* @returns {Object} Copy of the original object sorted by keys.*/function getSortedObject(object) {// New object which will be returned with sorted keysvar sortedObject = {};
// Get array of keys from the old/current objectvar keys = Object.keys(object);// Sort keys (in place)keys.sort();
// Use sorted keys to copy values from old object to the new onefor (var i = 0, size = keys.length; i < size; i++) {key = keys[i];value = object[key];sortedObject[key] = value;}
// Return the new objectreturn sortedObject;}
/*** Test run*/var unsortedObject = {d: 4,a: 1,b: 2,c: 3};
var sortedObject = getSortedObject(unsortedObject);
for (var key in sortedObject) {var text = "Key: " + key + ", Value: " + sortedObject[key];var paragraph = document.createElement('p');paragraph.textContent = text;document.body.appendChild(paragraph);}
// Only numbers to show it will be sorted.const testObj = {'2000': 'Articel1','4000': 'Articel2','1000': 'Articel3','3000': 'Articel4',};
// I'll explain what reduces does after the answer.console.log(Object.keys(testObj).reduce((accumulator, currentValue) => {accumulator[currentValue] = testObj[currentValue];return accumulator;}, {}));
/*** expected output:* {* '1000': 'Articel3',* '2000': 'Articel1',* '3000': 'Articel4',* '4000': 'Articel2'* }*/
// if needed here is the one liner:console.log(Object.keys(testObj).reduce((a, c) => (a[c] = testObj[c], a), {}));
// String exampleconst testObj = {'a1d78eg8fdg387fg38': 'Articel1','z12989dh89h31d9h39': 'Articel2','f1203391dhj32189h2': 'Articel3','b10939hd83f9032003': 'Articel4',};// Chained sort into all of this.console.log(Object.keys(testObj).sort().reduce((accumulator, currentValue) => {accumulator[currentValue] = testObj[currentValue];return accumulator;}, {}));
/*** expected output:* {* a1d78eg8fdg387fg38: 'Articel1',* b10939hd83f9032003: 'Articel4',* f1203391dhj32189h2: 'Articel3',* z12989dh89h31d9h39: 'Articel2'* }*/
// again the one liner:console.log(Object.keys(testObj).sort().reduce((a, c) => (a[c] = testObj[c], a), {}));
如果有人想知道Reduce是做什么的:
// Will return Keys of object as an array (sorted if only numbers or single strings like a,b,c).Object.keys(testObj)
// Chaining reduce to the returned array from Object.keys().// Array.prototype.reduce() takes one callback// (and another param look at the last line) and passes 4 arguments to it:// accumulator, currentValue, currentIndex and array.reduce((accumulator, currentValue) => {
// setting the accumulator (sorted new object) with the actual property from old (unsorted) object.accumulator[currentValue] = testObj[currentValue];
// returning the newly sorted object for the next element in array.return accumulator;
// the empty object {} ist the initial value for Array.prototype.reduce().}, {});
如果需要,这里是一个衬垫的解释:
Object.keys(testObj).reduce(
// Arrow function as callback parameter.(a, c) =>
// parenthesis return! so we can safe the return and write only (..., a);(a[c] = testObj[c], a)
// initial value for reduce.,{});
// This is just to show what happens, please don't use symbols in keys.const testObj = {'1asc': '4444',1000: 'a',b: '1231','#01010101010': 'asd',2: 'c'};
console.log(Object.keys(testObj));// output: [ '2', '1000', '1asc', 'b', '#01010101010' ]