我試圖從這個數組中獲取唯一的URL。JavaScript - 如何從基於唯一#的數組中獲取唯一的url?
var urls = [
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
]
從代碼,第五陣列['791','246','796','051', '056']
,所以輸出是這樣的:
[
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
]
從另一個答案,我能搶就在數組中的唯一第五碼:
var unique = ['791','246','796', '051','056']
然後我試圖做到這一點,以抓取唯一的URL並將其推送到一個空的數組,但它不工作。
var uniqueUrls = []
for(i=0; i < unique.length; i++){
for(j=0; j< urls.length; j++){
if(urls[j].indexOf(unique[i])){
uniqueUrls.push(urls[j]);
break;
}
}
}
任何人都可以幫助我嗎?我在for循環中做錯了什麼?或者是否有更好的方法來從URL數組中抓取唯一的URL而不使用double for循環?
更新
忘記提到其他位代碼並不總是「零」,也可以是任意的隨機數,但我仍然有興趣在抓住以第五代碼唯一的網址。
1.'.indexOf(unique [i])'應該是'> -1'。 2)爲什麼'打破'? –
@mparnisari我添加了中斷,所以一旦找到它,它會轉到下一個「唯一」值。也許有更好的辦法。 – medev21