I have a list of url's in WordPress that needs to be sorted in an efficient way through loop.
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]);
});
I tried to output the json object with forEach then split the url so I can get the second part of the url which is cat1, cat2, cat3. Each url has no definite length.
Do you know how can I achieve the output below? I am somehow aiming to do it inside the forEach loop.
https://example.com/cat1
https://example.com/cat1
https://example.com/cat1
https://example.com/cat2
https://example.com/cat2
https://example.com/cat3
Note: The categories is NOT limited to cat1,cat2,cat3. It could be also https://example.com/news or https://example.com/events
Any help is greatly appreciated. Thanks.