javascript - 返回从多行文本文件中获得的拆分字符串
问题描述
我试图通过读取文本文件并在逗号后弹出所有内容来返回从文本文件中获得的数据。我尝试使用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)
解决方案
您需要按行拆分,然后在,
使用后获取每个项目map
const txt = `orange,001
bannana,002
apples,003`
const result = txt.split("\n").map(x => x.split(",")[1])
console.log(result);
推荐阅读
- excel - 如何在excel中的单个堆叠水平条形图上绘制一系列起点和终点
- c++ - 如何实现线程列表
- reactjs - Mobx 和反应。不支持实验语法
- angular - 等效于 Angular 中 C# 的简单“for”(使用索引迭代)
- python - 在嵌套列表python中查找所有可能的插入组合
- jquery - Ajax 成功刷新(如阴影)中的 Bootstrap 模态弹出窗口比消失 MVC
- reactjs - React js Axios 调用
- java - Android Studio:从海量数据中自动完成 EditText 输入
- mars-simulator - 火星模拟器数字实验室模拟中键盘的使用
- angular - 使用切片管道后如何连接字符串