Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用下一行执行类似于forEach()的操作?_Javascript_Node.js_Foreach_Newline - Fatal编程技术网

Javascript 如何使用下一行执行类似于forEach()的操作?

Javascript 如何使用下一行执行类似于forEach()的操作?,javascript,node.js,foreach,newline,Javascript,Node.js,Foreach,Newline,如果我使用以下代码: 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_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"
假设每一行都是\n当然

我运行以下代码:

let dorks = original.split('/').pop().split('?')[0];
console.log(dorks)
它只返回:

index.php作为第一个url

我怎样才能使每一行都返回

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/是一个正则表达式,用于检测所有类型的空格。通过在所有空格的位置拆分字符串来创建数组后,您可以使用已经拆分的“?”再次拆分每个值

确保字符串的方案与此完全相同。否则,此脚本可能会抛出错误

/\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`; 设re=/[^.\/]+\.?:php | asp/g; console.logoriginal.matchre;
console.logoriginal.matchre.join'' 我不完全确定您希望如何显示匹配,但以下是如何避免循环:

让原件=`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`; 设re=/[^.\/]+\.?:php | asp/g; console.logoriginal.matchre; console.logoriginal.matchre.join'' 这里有一个forEach可以帮你做到这一点:

//请注意,此处使用了反勾号以允许字符串中的新行字符 让原件=`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` //在换行符上拆分,然后针对每个URL,在?并修剪额外的空间 original.split\n.forEachurl=>console.logurl.split?[0]。替换//g 这里有一个forEach可以帮你做到这一点:

//请注意,此处使用了反勾号以允许字符串中的新行字符 让原件=`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 c://https atalog.prm catalog.com/index.php?lang=tw&ID=18&CatalogID=2839 https://astacology.org/AboutCrayfish.asp?uid=Guest` //在换行符上拆分,然后针对每个URL,在?并修剪额外的空间
original.split\n.forEachurl=>console.logurl.split?[0]。替换//g 有几种方法可以得到你想要的东西。假设代码的变化最小,您的问题就是在多行上应用您的逻辑

因此,最简单的方法就是使用新行作为分隔符拆分字符串,然后为每个字符串应用您已经编写的代码:

让我们=`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`; 让dorks=s.split\n.mapurl=>url.split'/'.pop.split'?'[0];
console.logdorks有几种方法可以获得您想要的东西。假设代码的变化最小,您的问题就是在多行上应用您的逻辑

因此,最简单的方法就是使用新行作为分隔符拆分字符串,然后为每个字符串应用您已经编写的代码:

让我们=`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`; 让dorks=s.split\n.mapurl=>url.split'/'.pop.split'?'[0]; 控制台,洛格多克斯 工作很好,我在新行拆分并映射函数


工作很好,我在新行拆分并映射函数

你要分手了?然后只捕获拆分数组[0]的第一个元素。您希望发生什么?在新行上拆分,然后使用.forEach或普通循环,或.map对每个URL执行相同的操作。您正在拆分?然后只捕获拆分数组[0]的第一个元素。您希望发生什么?在新行上拆分,然后使用.forEach或普通循环,如果使用一个特定的答案帮助你实现这个结果,那么考虑使用答案旁边的复选标记来接受它,只需添加你的最终解决方案作为注释。如果一个特定的答案帮助你实现这个结果,那么考虑使用答案旁边的复选标记来接受它。只需添加您的最终解决方案作为注释。
let mapped = original.split('\n').map(o => o.split('/').pop().split('?')[0])