我有一个文本文件,其中包含一些我需要从不同行提取的数字。数字序列并不总是相同,所以我需要循环,直到没有更多的数字。以下示例。
数字:1,2,3,4(222.000,222.000),(333.000,222.000),.....,
需要在这里创建一个循环(下面)
output1=output1.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[7]).join("\n");
output2=output2.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[8]).join("\n");
output3=output3.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[9]).join("\n");
output4=output4.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[10]).join("\n");
output5=output5.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[11]).join("\n");
output6=output6.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[12]).join("\n");
output7=output7.split("\n").filter(/./.test, /Numbers/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[13]).join("\n");
答案 0 :(得分:0)
您可以创建一个函数来对字符串输入返回.split()
,.map()
,.filter()
次调用。创建一个具有属性名称的对象,反映索引的值对以括号表示法设置为属性,字符串输入为值。使用.forEach()
迭代对象数组,在每个对象上调用函数。
function filter(str, index) {
return str.split("\n").filter(/./.test, /Numbers/)
.map(line => line.split(/,|\(|\)/)
.filter(number => number != "")[index]).join("\n")
};
[output1, output2, output3, output4, output5, output6, output7] =
[{7:output1}, {8:output2}, {9:output3}, {10:output4}
, {11:output5} {12:output6}, {13:output7}]
.map(o => filter.apply(null, Object.entries(o).pop()));