遍历数组并在 Javascript/Nodejs 中输出模板 string/literals
Looping over an array and outputting a template string/literals in Javascript/Nodejs
我是 Javascript 的新手,我只是想遍历一个数组,将循环迭代的值附加到模板字符串,然后通过 console.log
打印它一个 nodejs 终端。
我编写了以下代码来制作我的数组:
// Get LTLAs from file
var fs = require("fs");
var text = fs.readFileSync("./LTLAs.txt", 'utf8');
var LTLAs = text.split("\n");
这给了我一个数组,见下面的片段:
'Barking and Dagenham\r'
'Babergh\r'
'Aylesbury Vale\r'
'Ashford\r'
'Ashfield\r'
'Arun\r'
'Amber Valley\r'
'Allerdale\r'
'Adur\r'
然后我循环这个数组:
for (let i = 0; i < LTLAs.length; i++)
{
const endpoint = `https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=ltla;areaName=${LTLAs[i]}&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}`;
console.log(endpoint);
}
我希望这能给我一组整洁的字符串,其中唯一的区别是 areaName=${LTLAs[i]}
在每个循环迭代中发生变化。相反,我得到一组不完整的字符串,只有最后一个循环实际上是正确的,如下所示:
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}Dagenham
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}le
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=ltla;areaName=Adur&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
如能提供任何有关使所有字符串遵循与最终字符串相同模式的帮助,我们将不胜感激。
提前致谢
您需要从字符串中删除回车 return 符号 ('\r') 以在节点 js 中获得正确的输出(某些浏览器可能会忽略它)。
您可以遍历数组:
var LTLAs = text.split("\n").map(string => string.replace('\r', ''));
或者你可以用 \r\n
代替 \n
:
var LTLAs = text.split("\r\n")
我是 Javascript 的新手,我只是想遍历一个数组,将循环迭代的值附加到模板字符串,然后通过 console.log
打印它一个 nodejs 终端。
我编写了以下代码来制作我的数组:
// Get LTLAs from file
var fs = require("fs");
var text = fs.readFileSync("./LTLAs.txt", 'utf8');
var LTLAs = text.split("\n");
这给了我一个数组,见下面的片段:
'Barking and Dagenham\r'
'Babergh\r'
'Aylesbury Vale\r'
'Ashford\r'
'Ashfield\r'
'Arun\r'
'Amber Valley\r'
'Allerdale\r'
'Adur\r'
然后我循环这个数组:
for (let i = 0; i < LTLAs.length; i++)
{
const endpoint = `https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=ltla;areaName=${LTLAs[i]}&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}`;
console.log(endpoint);
}
我希望这能给我一组整洁的字符串,其中唯一的区别是 areaName=${LTLAs[i]}
在每个循环迭代中发生变化。相反,我得到一组不完整的字符串,只有最后一个循环实际上是正确的,如下所示:
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}Dagenham
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}le
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=ltla;areaName=Adur&structure={"ltla":"areaName","date":"date","Rate":"newCasesBySpecimenDateRollingRate"}
如能提供任何有关使所有字符串遵循与最终字符串相同模式的帮助,我们将不胜感激。
提前致谢
您需要从字符串中删除回车 return 符号 ('\r') 以在节点 js 中获得正确的输出(某些浏览器可能会忽略它)。
您可以遍历数组:
var LTLAs = text.split("\n").map(string => string.replace('\r', ''));
或者你可以用 \r\n
代替 \n
:
var LTLAs = text.split("\r\n")