json 串排序
//对json的key排序 sortObjByKey:(obj)=>{ var keys =Object.keys(obj).sort(); var newObj ={}; for(var i =0;i<keys.length;i++){ var index = keys[i]; newObj[index] =obj[index]; } return JSON.stringify(newObj); },
let json = {nasd:'123',bad:'dsa',age:21,obj:'dsa'} //复杂json的key排序 sortAesc:(obj)=>{ const sortObj = {}; let Objarr = {}; let arrChild =[]; Object.keys(obj).sort().forEach(item=>{ if(obj[item] instanceof Array){ obj[item].forEach(index=>{ Object.keys(index).sort().forEach(indexKey=>{ Objarr[indexKey] = index[indexKey]; }) arrChild.push(Objarr); Objarr ={} }); obj[item] = arrChild; } sortObj[item] = obj[item]; }) return sortObj; }
let json = {code:200,total:10,page:5,list:[{name:21,age:12,desc:'dasdas'}]}