gpt4 book ai didi

javascript - 有没有办法将 csv 数据转换为值数组

转载 作者:行者123 更新时间:2023-12-02 23:42:37 26 4
gpt4 key购买 nike

我添加了 jsfiddel 链接。到目前为止我尝试过的。任何帮助,将不胜感激。谢谢你

function onFileSelect(input) {
//this.itemHeaderName = itemHeaderName;
var files = input.files;
var csvData;
if (files && files.length) {
var fileToRead = files[0];
var fileReader = new FileReader();
fileReader.readAsText(fileToRead, 'UTF-8');
fileReader.onloadend = function(x) {
csvData = fileReader.result;
onFileLoad(csvData);
}

}
}

function onFileLoad(fileLoadedEvent) {
var csvSeparator = ',';
var textFromFileLoaded = fileLoadedEvent;
var csv = [];
var rows = textFromFileLoaded.split('\n');

_.forEach(rows, function(element) {
var col = [];
col = element.split(csvSeparator);
csv.push(col);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

<input id="uploadFile_hier" type="file" class="upload" onclick="onFileSelect(event.target)" multiple="false" />

CSV 文件数据:

Part    Property    Level   Comments
Mercury Sub 2 TOP,Bottom,5678
copper Material 1 KM
Iron Thickness 4 NA,KA

结果:

\["part", "Property", "Level", "Comments"\]
\["Mercury", "Sub-Compound", "2", ""TOP", "NOZZLE", "BLOCK", "9100""\]
\["copper", "Material", "1", "KM"\]
\["Iron", "Thickness", "4", ""NA", "KA""\]

预期:

\["part", "Property", "Level", "Comments"\]
\["Mercury", "Sub-Compound", "2", "TOP,NOZZLE,BLOCK,9100"\]
\["copper", "Material", "1", "KM"\]
\["Iron", "Thickness", "4", "NA,KA"\]][1]

https://jsfiddle.net/eswar786/t5mpojr2/

最佳答案

rows.forEach(element => {
if (element.indexOf('"') >= 0) {
const str = element.slice(element.indexOf('"'), element.length);
const str1 = element.slice(0,element.indexOf('"'));
element = str1 + str.replace(/[',]+/g, '-');
}

内部循环从数组中取出每个值并找出出现双引号的位置,然后将其替换为 -

关于javascript - 有没有办法将 csv 数据转换为值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56002786/

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