1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function getTagRank(ranks){ if(typeof ranks !== 'number'){ throw new Error('typeerror:获取标签的数量必须是number类型'); return; } let tags = [...document.querySelectorAll('*')].map(node=>node.nodeName); let obj = {}; tags.forEach(item =>{ if(!obj[item]){ obj[item] = 1; }else{ obj[item] += 1; } }); let tagRank = Object.keys(obj).sort((a,b)=>{ return obj[b] - obj[a]; }); tagRank.length = ranks; return tagRank; }
|