越简单越好!

Javascript下对对象进行按key值排序

发表于 2016-04-19 13:33 | 163次阅读 0次点赞   JavaScript
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];
  alert(k + ':' + myObj[k]);
}
返回顶部 ^