亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

長風秋雁 2023-07-14 15:05:52
如果我使用以下代碼: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=1http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Saxhttp://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7http://www.fsrm.ch/doc/c474.php?lang=e&id=474https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839https://astacology.org/AboutCrayfish.asp?uid=Guest"當然假設每個新行都是 \n我運行這段代碼:let dorks = original.split('/').pop().split('?')[0];console.log(dorks)它只返回:index.php(對于第一個網址)我怎樣才能讓它一起返回每一行?
查看完整描述

4 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

這是一個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,'')));


查看完整回答
反對 回復 2023-07-14
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

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


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


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)

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

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

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


查看完整回答
反對 回復 2023-07-14
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

我不完全確定您希望如何顯示匹配項,但以下是避免循環的方法:


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(' '));


查看完整回答
反對 回復 2023-07-14
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

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/是一個檢測各種空白的正則表達式。通過在所有空格的位置進行拆分來從字符串創建數組后,您可以按照'?'您已經執行的操作再次拆分每個值。


確保字符串的方案與此完全相同。否則這個腳本可能會拋出錯誤。


查看完整回答
反對 回復 2023-07-14
  • 4 回答
  • 0 關注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號