【发布时间】:2019-07-01 13:29:41
【问题描述】:
我有一个 WordPress 中的 url 列表,需要通过循环以有效的方式进行排序。
var urlList = [
{
"URL": "https://example.com/cat1/aa/bb/cc",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://example.com/cat2/aa",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://example.com/cat1/aa/bb/cc/dd/ee",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://example.com/cat3/aa/bb/cc/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://example.com/cat2/aa/bb",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://example.com/cat1/aa/bb",
"Last crawled": "Jun 23, 2019"
}
]
urlList.forEach(function(item) {
var myUrl = item.URL.split("/");
console.log("https://example.com/" + myUrl[3]);
});
我尝试输出带有forEach 然后split url 的json 对象,这样我就可以获得url 的第二部分,即cat1, cat2, cat3。每个 url 没有明确的长度。
你知道我怎样才能实现下面的输出吗?我的目标是在 forEach 循环内完成。
https://example.com/cat1
https://example.com/cat1
https://example.com/cat1
https://example.com/cat2
https://example.com/cat2
https://example.com/cat3
注意:类别不限于cat1,cat2,cat3。也可以是https://example.com/news 或https://example.com/events
非常感谢任何帮助。谢谢。
【问题讨论】:
-
那么,为什么不直接
.sort()?
标签: javascript jquery html css wordpress