Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

寻找出现次数最多的三个标签 #97

Open
Sunny-117 opened this issue Nov 3, 2022 · 3 comments
Open

寻找出现次数最多的三个标签 #97

Sunny-117 opened this issue Nov 3, 2022 · 3 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@CwRv07
Copy link
Contributor

CwRv07 commented Mar 8, 2023

// 寻找当前页面中出现最多的三个标签

// 取所有元素(两种方法)
  const pageDomList = Array.from(document.all ?? document.querySelectorAll('*'));
// 哈希计数
  const domMap = new Map();
  pageDomList.forEach(dom => {
    domMap.set(dom.tagName, (domMap.get(dom.tagName) ?? 0) + 1);
  })
// 排序取前三输出
  const mapEntriesList=Array.from(domMap);
  mapEntriesList.sort((a,b)=>b[1]-a[1]);
  console.log(mapEntriesList.slice(0,3));

@veneno-o
Copy link
Contributor

veneno-o commented Mar 11, 2023

function findThree(){
    const doms = Array.from(document.querySelectorAll("*")).map(node => node.tagName);
    const map = new Map();
    for (const item of doms) {
        map.has(item) ? map.set(item, map.get(item) + 1) : map.set(item, 1);
    }
    return Array.from(map.entries()).sort((a, b) => b[1] - a[1]).slice(0, 3);
}

@kangkang123269
Copy link

function findTopThreeTags() {
    const doms = Array.from(document.querySelectorAll("*")).map(node => node.tagName);
    const tagCountMap = new Map();

    for (const tag of doms) {
        if(tagCountMap.has(tag)) {
            tagCountMap.set(tag, tagCountMap.get(tag) + 1);
        } else {
            tagCountMap.set(tag, 1);
        }
    }

    return Array.from(tagCountMap.entries())
                 .sort((a, b) => b[1] - a[1])
                 .slice(0, 3)
                 .map(entry => ({tag: entry[0], count: entry[1]}));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants