我用来papaparse读取 csv 文件,然后将该文件转换为对象数组。我用来react-dropzone上传文件,然后将其转换为对象数组。但我需要所有标题均为小写,某些列之间没有空格才能以数组形式存储数据。
这是我的 csv 文件
| Name | Age | Data one | Data two |
-------|-----|----------|----------|
| John | 23 | A, B | C |
-------|-----|----------|----------|
| Jane | 40 | E, F | G, H |
-------|-----|----------|----------|
这是我的代码:
import React from "react";
import Dropzone from "react-dropzone";
import Papa from 'papaparse';
const App = () => {
const handleOnDrop = (acceptedFiles) => {
// acceptedFiles is the actual file uploaded by user
Papa.parse(acceptedFiles[0], {
header: true,
complete: (results) => {
console.log(results)
}
})
}
}
OutPut:
[
{
Name: "John",
Age: "23",
Data one: "A, B",
Data two: "C"
},
{
Name: "Jane",
Age: "40",
Data one: "E, F",
Data two: "G, H"
}
]
我需要的输出是:
[
{
name: "John",
age: "23",
dataone: ["A", "B"], // Each individual data should be a string
datatwo: ["C"]
},
{
name: "Jane",
age: "40",
dataone: ["E", "F"],
datatwo: ["G", "H"]
}
]
我是新手papaparse,不确定它是如何完成的
蓝山帝景
相关分类