Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将CSV转换为JS对象数组_Javascript_Csv - Fatal编程技术网

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

Javascript 如何将CSV转换为JS对象数组,javascript,csv,Javascript,Csv,在将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","N

在将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.entriesobj;或Object.keysobj;或者将其转换为array first array.fromobj,但未获得预期结果

最重要的是,所有方法都将每个单词分隔为单个字符,如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,那么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字符串都用双引号括起来,但是名称7也用双引号括起来,加了一些斜杠,而不是4个逗号分隔的值,我们有5个逗号分隔的值

我建议使用专用的CSV解析器,比如,这正是它们的设计目的

一旦你开始引用CSV数据,解析CSV数据就会变得非常棘手

让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`; 让result=Papa.parsecsv,{header:true,dynamicTyping:true}; 控制台。日志结果:,结果;
有几种方法可以做到这一点。下面是一个片段,它使用了一个用于csv行的。如果csv变得更复杂,例如嵌套等,您可能需要编写一个解析器,这很有趣!或用于解析csv文件

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`.split\n; //获取标题 const headers=csv[0]。拆分,; //帮助器从值创建行 //使用刚刚创建的标题 const createRow=values=>headers.reduce acc,header,i=> {…acc,[头]:值[i]},{}; //将csv行减少为对象数组 常数csv2Obj=csv .1//当然不需要标题 .reduce acc,row=>[…acc,createRowrow.replace//g、.split、]、[]; console.logcsv2Obj;
.作为控制台包装器{top:0;max height:100%!important;}以下是我对qoutes的具体示例的尝试

const parseCsv=csv=>{ let line=csv.split\n; const header=lines.shift.split, return lines.mapline=>{ const bits=JSON.parse[+line+] 设obj={}; header.forEachh,i=>obj[h]=位[i];//或在此处使用reduce //可选: obj[includeInAuto]=obj[includeInAuto]==true; 返回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.logparseCsvcsv 这是我解决这个问题的方法

让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`; 让数据=csv.split\n; 设title=data.shift.split,; 让数组=data.reducearr,行=>{ row=row.replace//g; 让innerObj=row.split、.reduceobj、item、index=>{ obj[标题[索引]]=项目; 返回obj; }, {}; arr.pushinnernobj; 返回arr; }, [];
console.logarray;谢谢Teemu,是的,这是一个CSV,我已经更新了我的问题。你可以使用这个很酷的库CSV解析,如果可以的话。谢谢你的回复,有任何机会请你建议一个手动方法,而不是JS库。解析CSV数据是一个不平凡的练习,所以使用现有的,经过良好测试的库肯定是我的首选,而不是编写定制的东西。使用此线程中提供的技术-我要问,为什么要为\n使用正则表达式?我想它是。。。习惯感谢您的回答,如果我将值括在引号中,则上述代码可以正常工作,否则我将得到一个错误异常SyntaxeError:JSON中的意外标记T位于位置1,我也已更新了我的问题。@gs650x csv2Obj应在csv值周围加引号或不加引号生成。您得到的错误与代码段无关,但与您正在使用JSON执行的操作有关。JSON不是给定代码段的一部分。@mplungjan感谢您的建议。我已经修复了。您的数字仍然是字符串,如果内容中有逗号,您的代码将失败:Test1,姓氏1,FirstName1,true,1,type1-这不是一个好主意:row=row.replace//g;查看我的版本以获得更好的方式谢谢Terry的回答,但我必须在Salesforce中使用此js代码,但是
我不能在script tag中使用第三方库,我必须下载第三方JS的zip文件,并在代码中给出该zip文件的引用,但如果我从papaparse的网站下载zip文件,则zip文件的大小约为13MB,但我不能使用超过5MB的zip文件,你能建议一下吗?PapaParse库应该比这个小很多,我认为它更像19kb-。你应该能够下载这个包括!评论不用于扩展讨论;这段对话已经结束。