我在WordPress中的JSON对象中有网址列表。我想计算网址第二部分的出现。
下面的代码当前获取前缀https://www.example.co
之后的其余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);
});
您知道如何在当前的forEach
循环中实现这一目标吗?
任何帮助将不胜感激。谢谢
答案 0 :(得分:1)
使用正则表达式匹配/
之后的非.co/
:
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);
在较新的引擎上,您可以使用lookbehind而不是提取捕获组:
const paths = urlList.map(
({ URL }) => URL.match(/(?<=\.co\/)[^\/]+/)[0]
);
如果要跟踪使用的所有完整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);