我想使用fs创建一个包含我的数组的.txt文件。
我的数组看起来如下:
const array = [1111, 2222, 33333]下面的代码允许我在文件中写入并用逗号分隔每个条目,但是我不能在新的行中获得它们。既不使用\n也不使用\r\n
await fs.writeFile('Participants.txt', `${array}, \r\n`);谢谢你提前帮忙。
发布于 2022-05-17 18:23:29
这就是你要找的东西吗?
const array = [1111, 2222, 33333]
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
fs.appendFile('Participants.txt', `${array[i]}\n`);
}发布于 2022-05-17 18:30:28
你要找的是array.join()
const text_string = eligible.join('\n');
await fs.writeFile('Participants.txt', text_string);在上面的示例中,为了清晰起见,我将要写入文件的字符串分隔开,这样就可以清楚地看到发生了什么。当然,您可以跳过额外的变量,直接在eligible.join('\n')中执行fs.writeFile(),但我想说明的是,这个特性与fs无关,与任何字符串语法或任何函数参数语法无关。它只是数组的一个特性。
https://stackoverflow.com/questions/72278898
复制相似问题