我在txt檔案中有陣列并用
fs.readFileSync("./input.txt")
當我將它包裝在 console.log 中時,我得到(因為它是寫在檔案本身中的):
1 2 3
100 5000
我會有陣列:
['1','2','3','100','5000']
陣列在輸入檔案中的位置不應改變。請建議如何做。
uj5u.com熱心網友回復:
您可以使用正則運算式來拆分單詞: \w
let a = ` 1 2 3
100 5000 `;
console.log(a.match(/\w /g))
要讀取您的檔案并將其拆分:
fs.readFileSync("./input.txt").match(/\w /g)
uj5u.com熱心網友回復:
一種方法,然后加載它,按行拆分,然后在每行按空間拆分然后將其展平,然后過濾掉空值。
let str = `1 2 3
100 5000 0`; // added 0 to show filter(Boolean) wont remove
console.log(str.split('\n').map(v => v.split(' ')).flat().filter(Boolean))
結果:
[
"1",
"2",
"3",
"100",
"5000",
"0"
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/392287.html
標籤:javascript 节点.js
