JS 找出字符串中每个字符出现的次数且去重
1.找出每个字符出现的次数
let obj = {}; let arr = ['a','a','a','b','b','c','d','e','f','f']; arr.map(x => obj[x]= obj[x] ? obj[x] + 1 : 1 ); console.log(obj);
2.字符去重,且,根据次数倒序显示
let orderObj = {}; Object.keys(obj).map(x=> { if(orderObj[obj[x]] == undefined) orderObj[obj[x]] = [x]; else orderObj[obj[x]].push(x); }) Object.keys(orderObj).reverse().map(x => console.log(orderObj[x].join(' ')))

浙公网安备 33010602011771号