vue、js前端map排序
objSortByVal(obj) { let keyArr = [],valArr = []; for (let key in obj) { keyArr.push(key); valArr.push(obj[key]); } for (let i = 0, len = valArr.length; i < len; i++) { for (let j = 0; j < len - i; j++) { let keyTemp, valTemp; if (valArr[j] < valArr[j + 1]) { valTemp = valArr[j]; valArr[j] = valArr[j + 1]; valArr[j+1] = valTemp; keyTemp = keyArr[j]; keyArr[j] = keyArr[j + 1]; keyArr[j+1] = keyTemp; } } } let newObj={}; for(let i=0;i<valArr.length;i++){ newObj[''+keyArr[i]+'']=valArr[i]; } return newObj; },
调用方法:
let newMap = this.objSortByVal(res.body.totalMap);