对象key值排序,以key值(数字)大小顺序遍历属性,helper._sort()

var helper = {
  _sort:function(data){ //{“20141216”:{},“20141217”:{}}按大小排序,
        var arr1 = [],arr2=[];
        for(var key in data){
            arr1.push(key);
        }
        arr1.sort(function(a,b){return a-b});
        for(var i=0;i<arr1.length;i++){
            arr2.push(data[arr1[i]]);
        }
        return arr2;
    }
}
var obj = { "12":"txt12", "13":"txt13", "4":"txt4", "9":"txt9", '01':'txt01', '05':'txt05' }
console.log(helper._sort(obj))
//["txt01", "txt4", "txt05", "txt9", "txt12", "txt13"]

 

posted @ 2014-12-16 14:31  刘金宇  阅读(395)  评论(0编辑  收藏  举报