我在WordPress
中的json对象中有一个URL列表。我想用一个前缀将它们分开,我可以看到它们是https://www.example.co
,然后得到其余部分,例如/cat1/aa/bb/cc, /?page_id=8830, /xmlrpc.php
var myUrl = [
{
"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/news/why-residents/attachment/san-francisco-blog/feed/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/xmlrpc.php",
"Last crawled": "Jun 19, 2019"
},
{
"URL": "https://www.example.co/?page_id=8830",
"Last crawled": "Jun 18, 2019"
},
]
我当前的代码如下:
myUrl.forEach(function(item) {
var urlList = item.URL.split("/");
//console.log(urlList);
console.log("https://www.example.co/" + urlList[3]);
});
上面的代码当前无法从前缀中获取其余部分。
任何帮助将不胜感激。谢谢
答案 0 :(得分:1)
如果您只想从URL中删除域,则可以使用String.prototype.replace
而不是String.prototype.split
。
例如:
const myUrl = [
{
"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/news/why-residents/attachment/san-francisco-blog/feed/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/xmlrpc.php",
"Last crawled": "Jun 19, 2019"
},
{
"URL": "https://www.example.co/?page_id=8830",
"Last crawled": "Jun 18, 2019"
},
];
const paths = myUrl.map(value => value.URL.replace('https://www.example.co', ''));
console.log(paths);
答案 1 :(得分:1)
如果数据来自不同的URL域,则可以创建URL
对象并获取pathname
和search
。
const paths = myUrl.map(value => new URL(value.URL).pathname + new URL(value.URL).search);
const myUrl = [
{
"URL": "https://www.microsoft.co/cat1/aa/bb/cc",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.google.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/news/why-residents/attachment/san-francisco-blog/feed/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/xmlrpc.php",
"Last crawled": "Jun 19, 2019"
},
{
"URL": "https://www.example.co/?page_id=8830",
"Last crawled": "Jun 18, 2019"
},
];
const paths = myUrl.map(value => new URL(value.URL).pathname + new URL(value.URL).search);
console.log(paths);