北漂IT民工 的博客

将javascript对象属性排序与遍历javascript属性的方法


var myobj = {‘2007’: {

‘5’: {

‘01’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘03’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘02’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}]

},

‘6’ : {

‘04’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘08’ : [{‘tit’:’dd’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘02’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}]

}

},

‘2006’: {

‘3’: {

‘01’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘03’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘02’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}]

},


‘4’: {

‘04’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘08’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}],

‘02’ : [{‘tit’:’abc’,’mon’:123,’typ’:2},{‘tit’:’abc’,’mon’:123,’typ’:2}]

}

}

};


//delete obj[‘2009’][‘5’][‘1’];


var sortObject = function(obj){

var tmpArr = [];


for(var key in obj){

if(obj[key].constructor == Object){

obj[key] = sortObject(obj[key]);

}

tmpArr.push(key);

}

tmpArr.sort();

var o = {}

for( var i = 0; i
o[tmpArr[i]] = obj[tmpArr[i]];

}

return o;

}


var newobj = sortObject(myobj);

var listObject = function(obj){

for(var i in obj){

alert(i);

}

for(var i in obj){

if(obj[i].constructor == Object){

listObject(obj[i]);

}

}

}

listObject(newobj);