简体   繁体   English

读取文件并创建要循环的数组,javascript

[英]Read a file and create array to loop through, javascript

I have a task to read a file with actions and values assigned to the actions.我的任务是读取一个文件,其中包含分配给这些操作的操作和值。 At the end the code is supposed to add some of the values together but also multiply some of the values, in a specific order.最后,代码应该以特定顺序将一些值相加,但也将一些值相乘。 But to start with I need to get the code to read the contents of my file as an array.但首先我需要获取代码以将我的文件内容作为数组读取。 This is what I have so far and I hope to get some help with my next step.这是我目前所拥有的,我希望在我的下一步中得到一些帮助。

const input = document.querySelector('input')
const fileReader = new FileReader()
fileReader.onload = (e) => {
  console.log(e.target.result)
  const data = csvToArray(e.target.result)
  console.log(data)
}


const csvToArray = (str, delimiter = ",") => {
  const headers = str.slice(0, str.indexOf("\n")).split(delimiter)

  const rows = str.slice(str.indexOf("\n") + 1).split("\n")


  const arr = rows.map((row) => {
    const values = row.split(delimiter)
    console.log(values)

const el = headers.reduce((object, header, index) => {
  headers[header] = values[index]
  return object
}, {})
return el


 })

  return arr
}

input.onchange = (e) => {
  console.log(e.target.result)
  const [file] = e.target.files
  fileReader.readAsBinaryString(file)

}

This code makes the content of my file appear in the console but it displayes the \r at the end of every row.此代码使我的文件的内容出现在控制台中,但它在每一行的末尾显示 \r 。 I have tried to add the "\r" where I put the "\n" but realised it was not the way to go. I also tried it with the delimiter but it was not right.我试图在我放置“\n”的地方添加“\r”,但意识到这不是通往 go 的方式。我也尝试过使用分隔符,但这是不正确的。 How do I remove the \r and create an array I can access with Javascript and loop through it?如何删除 \r 并创建一个我可以使用 Javascript 访问的数组并循环遍历它?

My file looks something like this:我的文件看起来像这样:

actions values行动价值观

f 4 4

u 2 u 2

f 8 8

d 1 1

f 2 2

u 3你 3

f 3 3

I would split the input into individual lines using regex:我会使用正则表达式将输入分成单独的行:

str = str.split(/[\r\n]+/);
const header = str.shift().split(delimiter);

This also grabs the header line.这也抓住了 header 行。 Then map the data.然后是map这个数据。 Complete code:完整代码:

const input = document.querySelector('input');
const fileReader = new FileReader();
fileReader.onload = (e) => {
    const data = csvToArray(e.target.result, ' ');
    console.log(data);
};

const csvToArray = (str, delimiter = ",") => {
    str = str.split(/[\r\n]+/);
    const header = str.shift().split(delimiter);
    return str.filter(line => line && line.includes(delimiter))
              .map(line => {
                  line = line.split(delimiter);
                  return {[header[0]]: line[0], [header[1]]: parseInt(line[1])};
              }
    );
};

input.onchange = (e) => {
    const [file] = e.target.files;
    fileReader.readAsText(file);
};

Output: Output:

[{
  actions: "f",
  values: 4
}, {
  actions: "u",
  values: 2
}, {
  actions: "f",
  values: 8
}, {
  actions: "d",
  values: 1
}, {
  actions: "f",
  values: 2
}, {
  actions: "u",
  values: 3
}, {
  actions: "f",
  values: 3
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM