首页 > 解决方案 > 返回从多行文本文件中获得的拆分字符串

问题描述

我试图通过读取文本文件并在逗号后弹出所有内容来返回从文本文件中获得的数据。我尝试使用split(',').pop(),但这仅适用于 1 个字符串。如何让它在文本文件中的每一行上进行迭代,然后返回弹出的字符串。我在下面举了一个例子。任何帮助表示赞赏。提前致谢。

Example txt file:
orange,001
bannana,002
apples,003

would return:
001
002
003

//something like this but for everyline in the text file.
const reader = fs.readFileSync('filePath', 'utf-8');
const popped = reader.split(',').pop();
console.log(popped)

标签: javascript

解决方案


您需要按行拆分,然后在,使用后获取每个项目map

const txt = `orange,001
bannana,002
apples,003`

const result = txt.split("\n").map(x => x.split(",")[1])
console.log(result);


推荐阅读