簡體   English   中英

如何執行類似於 forEach() 但使用下一行的操作?

[英]How can I perform something similar to forEach() but using next line?

如果我使用以下代碼:

let original = "http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56&
http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1
http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7
https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax
http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1
http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1
http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7
http://www.fsrm.ch/doc/c474.php?lang=e&id=474
https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839
https://astacology.org/AboutCrayfish.asp?uid=Guest"

假設每個新行都是 \\n

我運行這個代碼:

let dorks = original.split('/').pop().split('?')[0];
console.log(dorks)

它只返回:

index.php(至於第一個網址)

我怎樣才能讓它為每一行都返回?

這是一個forEach會為你做的:

 //note the use of a backtick here to allow your new line characters in string let original = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56& http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1 http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7 https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1 http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1 http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7 http://www.fsrm.ch/doc/c474.php?lang=e&id=474 https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839 https://astacology.org/AboutCrayfish.asp?uid=Guest` //split on newline, and then for each URL, grab everything before the ? and trim extra spaces original.split("\\n").forEach((url)=> console.log(url.split("?")[0].replace(/ /g,'')));

let original = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56&
http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1
http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7
https://www.music-scores.com/sheet-music/freeinstrument.php? instrument=Alto%20Sax
http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1
http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1
http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7
http://www.fsrm.ch/doc/c474.php?lang=e&id=474
https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839
https://astacology.org/AboutCrayfish.asp?uid=Guest`

original.split(/\s/).map(w => w.split('?')[0])
/* returns [
     "http://www.spur-g-shop.de/index.php", 
     "http://associations.beauvais.fr/en-un-clic/index.php",
     "http://laptopbank.net/product_detail.php",
     "https://www.music-scores.com/sheet-music/freeinstrument.php",
     "http://www.traxjo.com/index.php",
     "http://www.bizfocus.co.kr/admin/bbs/down.php",
     "http://www.vivitekthailand.com/en/Product_view.php",
     "http://www.fsrm.ch/doc/c474.php",
     "https://catalog.prm-catalog.com/index.php",
     "https://astacology.org/AboutCrayfish.asp"
   ] */

/\\s/是一個正則表達式,可以檢測各種空格。 通過在所有空格的位置拆分從字符串創建數組后,您可以再次通過'?'拆分每個值'?' 你已經這樣做了。

確保字符串的方案完全一樣。 否則此腳本可能會引發錯誤。

我不完全確定您希望如何顯示匹配項,但您可以通過以下方式避免循環:

 let original = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56& http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1 http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7 https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1 http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1 http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7 http://www.fsrm.ch/doc/c474.php?lang=e&id=474 https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839 https://astacology.org/AboutCrayfish.asp?uid=Guest`; let re = /[^.\\/]+\\.(?:php|asp)/g; console.log(original.match(re)); console.log(original.match(re).join(' '));

有幾種方法可以獲得你想要的東西。 假設代碼更改最少,您的問題只是在多行上應用您的邏輯。

因此,最簡單的方法是使用新行作為分隔符拆分字符串,然后為每個字符串應用您已經編寫的代碼:

 let s = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56& http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1 http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7 https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1 http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1 http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7 http://www.fsrm.ch/doc/c474.php?lang=e&id=474 https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839 https://astacology.org/AboutCrayfish.asp?uid=Guest`; let dorks = s.split("\\n").map(url => url.split('/').pop().split('?')[0]); console.log(dorks)

請注意模板文字的用法,以便輕松獲取新行。

正如您所看到的,主要邏輯與您編寫的完全相同( url.split("/").pop().split("?")[0] )它只是使用map應用於每一行。

您可以使用正則表達式來解決這個問題,但我認為這對您了解如何在多行上應用相同的邏輯很有幫助(因此,如果邏輯發生變化,您可以輕松更改傳遞給map的函數。

let mapped = original.split('\n').map(o => o.split('/').pop().split('?')[0])

工作正常,我在新行拆分並映射了函數!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM