gpt4 book ai didi

javascript - 如何将 CSV 转换为 JS 对象数组

转载 作者:行者123 更新时间:2023-12-02 02:53:59 25 4
gpt4 key购买 nike

将 CSV 文件转换为 JS 对象后,我得到以下结构

"category,name,includeInAuto,srNumber,testType
"Test1","Name1","true",1,"type1"
"Test2","Name2","true",1,"type2"
"Test3","Name3","true",1,"type3"
"Test4","Name4","true",1,"type4"
"Test5","Name5","true",1,"type5"
"Test6","Name6","true",1,"type6"
"Test7","Name7","true",1,"type7"

我正在尝试像下面这样转换它

[{"category": "Test1", "name": "Name1", "includeInAuto": "true", "srNumber": 1 "testType": "type1"},
{"category": "Test2", "name": "Name2", "includeInAuto": "true", "srNumber": 2 "testType": "type2"},
{"category": "Test3", "name": "Name3", "includeInAuto": "true", "srNumber": 3 "testType": "type3"},
{"category": "Test4", "name": "Name4", "includeInAuto": "true", "srNumber": 4 "testType": "type4"},
{"category": "Test5", "name": "Name5", "includeInAuto": "true", "srNumber": 5 "testType": "type5"},
{"category": "Test6", "name": "Name6", "includeInAuto": "true", "srNumber": 6 "testType": "type6"},
{"category": "Test7", "name": "Name7", "includeInAuto": "true", "srNumber": 7 "testType": "type7"}]

我试过使用 Object.entries(obj);Object.keys(obj); 之类的 map ,或者先将其转换为数组 Array.from (obj) 但没有得到预期的结果。

以上所有方法将每个单词分隔为单个字符,如类别为“c”、“a”、“t”、“e”、“g”、“o”、“r”、“y”

有人可以帮我实现同样的目标吗?

更新

如果我在 excel 中编辑 csv 文件,然后尝试解析它,那么我会得到一个下面的结构,其中不是用双引号将所有值括起来,而是将整个数据用双引号括起来,如下所示

"category,name,includeInAuto,srNumber,testType 
Test1,Name1,true,1,type1
Test2,Name2,true,2,type2
Test3,Name3,true,3,type3
Test4,Name4,true,4,type4
Test5,Name5,true,5,type5
Test6,Name6,true,6,type6
Test7,Name7,true,7,type7"

如果不是上面的任何值在其中有任何特殊字符让我们假设如果我将 name7 更改为 name,7 然后 FileReader 返回下面的结构

"category,name,includeInAuto,srNumber,testType 
Test1,Name1,true,1,type1
Test2,Name2,true,2,type2
Test3,Name3,true,3,type3
Test4,Name4,true,4,type4
Test5,Name5,true,5,type5
Test6,Name6,true,6,type6
Test7,\"Name,7\",true,6,type6"

在上面的整个 csv 字符串中是双引号,但名称 name, 7 也是双引号和一些额外的斜线,而不是 4 个逗号分隔值,我们有 5 个逗号分隔值。

最佳答案

这是我用 qoutes 对你的具体例子的尝试

const parseCsv = csv => {
let lines = csv.split("\n");
const header = lines.shift().split(",")
return lines.map(line => {
const bits = JSON.parse("[" + line + "]")
let obj = {};
header.forEach((h, i) => obj[h] = bits[i]); // or use reduce here
// optional:
obj["includeInAuto"] = obj["includeInAuto"] === "true";
return obj;
});
};

const csv = `category,name,includeInAuto,srNumber,testType
"Test1","Name1","true",1,"type1"
"Test2","Name2","true",1,"type2"
"Test3","Name3","true",1,"type3"
"Test4","Name4","true",1,"type4"
"Test5","Name5","true",1,"type5"
"Test6","Name6","true",1,"type6"
"Test7","Name7","true",1,"type7"`

console.log(parseCsv(csv));

没有引号:

const parseCsv = csv => {
let lines = csv.split(/\r?\n/);
const header = lines.shift().split(",")
return lines.map(line => {
const bits = line.split(",")
let obj = {};
header.forEach((h, i) => obj[h] = bits[i]); // or use reduce here
// optional:
obj["includeInAuto"] = obj["includeInAuto"] === "true";
return obj;
});
};

const csv = `category,name,includeInAuto,srNumber,testType
Test1,Name1,true,1,type1
Test2,Name2,true,1,type2
Test3,Name3,true,1,type3
Test4,Name4,true,1,type4
Test5,Name5,true,1,type5
Test6,Name6,true,1,type6
Test7,Name7,true,1,type7`


console.log(parseCsv(csv));

带转义引号

const parseCsv = csv => {
let lines = csv.slice(1,csv.length-1).split(/\r?\n/);
console.log(lines)
const header = lines.shift().split(",")
return lines.map(line => {
const bits = line.trim().split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
let obj = {};
header.forEach((h, i) => obj[h] = bits[i].replace(/\"/g,"")); // or use reduce here
// optional:
obj["includeInAuto"] = obj["includeInAuto"] === "true";
return obj;
});
};

const csv = `"category,name,includeInAuto,srNumber,testType
Test1,Name1,true,1,type1
Test2,Name2,true,2,type2
Test3,Name3,true,3,type3
Test4,Name4,true,4,type4
Test5,Name5,true,5,type5
Test6,Name6,true,6,type6
Test7,\"Name,7\",true,6,type6"`


console.log(parseCsv(csv));

关于javascript - 如何将 CSV 转换为 JS 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61473549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com