简体   繁体   English

如何从 JavaScript 中的 url 列表计算单词出现次数?

[英]How do I count word occurence from a list of urls in JavaScript?

I have list of urls in a JSON object in WordPress.我在 WordPress 的 JSON 对象中有 url 列表。 I want to count the occurence of the second part of the url.我想计算 url 的第二部分的出现。

The code below currently gets the rest of the url after the prefix https://www.example.co .下面的代码当前获取前缀https://www.example.co之后的其余 url。 What I want to do next is the count the occurence of the second part of the url which is cat1, cat3, cat2, xmlrpc.php我接下来要做的是计算 url 的第二部分出现的次数,即cat1, cat3, cat2, xmlrpc.php

var urlList = [
  {
    "URL": "https://www.example.co/cat1/aa/bb/cc",
    "Last crawled": "Jun 23, 2019"
  },
  {
    "URL": "https://www.example.co/cat2/aa",
    "Last crawled": "Jun 23, 2019"
  },
  {
    "URL": "https://www.example.co/cat1/aa/bb/cc/dd/ee",
    "Last crawled": "Jun 23, 2019"
  },
  {
    "URL": "https://www.example.co/cat3/aa/bb/cc/",
    "Last crawled": "Jun 23, 2019"
  },
  {
    "URL": "https://www.example.co/cat2/aa/bb",
    "Last crawled": "Jun 23, 2019"
  },
  {
    "URL": "https://www.example.co/cat1/aa/bb",
    "Last crawled": "Jun 23, 2019"
  },
  {
    "URL": "https://www.example.co/xmlrpc.php",
    "Last crawled": "Jun 19, 2019"
  }
]

const paths = urlList.map(value => value.URL.replace('https://www.example.co', ''));

//console.log(paths);

paths.forEach(function(item) {
    var urlSecondPart = item.split("/")[1];
    console.log(urlSecondPart);
});

Do you know how can I achieve that with my current forEach loop?您知道如何使用当前的forEach循环实现这一目标吗?

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks谢谢

Use a regular expression to match non- / s that come after the .co/ :使用正则表达式匹配.co/之后的非/ s :

 var urlList = [ { "URL": "https://www.example.co/cat1/aa/bb/cc", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat2/aa", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat1/aa/bb/cc/dd/ee", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat3/aa/bb/cc/", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat2/aa/bb", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat1/aa/bb", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/xmlrpc.php", "Last crawled": "Jun 19, 2019" } ] const paths = urlList.map( ({ URL }) => URL.match(/\\.co\\/([^\\/]+)/)[1] ); console.log(paths); const counts = paths.reduce((a, str) => { a[str] = (a[str] || 0) + 1; return a; }, {}); console.log(counts);

On newer engines, you can use lookbehind instead of extracting the capture group:在较新的引擎上,您可以使用后视而不是提取捕获组:

const paths = urlList.map(
  ({ URL }) => URL.match(/(?<=\.co\/)[^\/]+/)[0]
);

If you want to keep track of all full URLs used, reduce not only into a count, but also into an array of those full URLs:如果要跟踪使用的所有完整 URL,不仅要减少计数,还要减少这些完整 URL 的数组:

 var urlList = [ { "URL": "https://www.example.co/cat1/aa/bb/cc", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat2/aa", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat1/aa/bb/cc/dd/ee", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat3/aa/bb/cc/", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat2/aa/bb", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/cat1/aa/bb", "Last crawled": "Jun 23, 2019" }, { "URL": "https://www.example.co/xmlrpc.php", "Last crawled": "Jun 19, 2019" } ] const getSecond = url => url.match(/\\.co\\/([^\\/]+)/)[1]; const counts = urlList.reduce((a, { URL }) => { const second = getSecond(URL); if (!a[second]) { a[second] = { count: 0, fullUrls: [] }; } a[second].count++; a[second].fullUrls.push(URL); return a; }, {}); console.log(counts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在字符串中出现“单词”的升序对字符串数组进行排序? javascript - How do I sort an array of strings, by the ascending occurence of a 'word' in the string? javascript 如何使用 javascript 中的 split 函数拆分值以只检查第一次和最后一次出现 - How do I split a value using the split function from javascript to split checking only the first and last occurence 如何获取并显示对象中每个单词的计数? - How do I get and display the count of each word from an object? 如何计算列表中添加的相同项目的出现? - how to count the occurence of same item added in the list? 如何在 javascript 中创建一个单词的所有可能字谜的列表? - How do I create a list of all possible anagrams of a word in javascript? 如何在 javascript 上保存文件 url? - How do I save file urls on javascript? 如何突出显示列表中的随机词 - How do I highlight a random word from a list 我在这段代码中遇到错误。我的想法是计算该单词在给定段落中重复出现的次数 - I'm getting error in this code.The idea is to calculate the count of occurence of how many times that word repeats in the given paragraph 如何在 javascript 中的每个特定单词出现处对长字符串进行切片 - How to slice a long string at every specific word occurence in javascript 如何在javascript中使单词变粗? - How do I make a word bold in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM