問
在 console
中統計淘寶首頁:
- 使用了多少種 HTML 標籤?
- 出現最多的三種標籤是什麼?各出現了多少次?
答
-
統計標籤總數
console.log(new Set([...document.querySelectorAll("*")].map(tag=>tag.tagName)).size)
-
統計出現最多的三種標籤及出現次數
Object.entries([...document.querySelectorAll("*")].map(tag=>tag.tagName).reduce((ret, i)=>{ ret[i] = (ret[i] || 0) + 1; return ret; }, {})).sort((a, b)=>(b[1] - a[1])).slice(0, 3) .map(a=>(`${a[0]}: ${a[1]}次`)).join(', ')