如何将Papa.parse结果放入数组 [英] How to get Papa.parse results into an array
问题描述
预警,我是js的新手,我制作的以下代码摘自 从Javascript对象中的CSV检索解析数据(使用爸爸解析)
Forewarning, I am new to js and I crafted the code below was taken from Retrieve parsed data from CSV in Javascript object (using Papa Parse)
我的目标=将一个csv文件解析为一个数组,并在其他一些操作中使用该数组.我可以看到文件已通过"console.log("Row:",row.data);正确解析,但是我无法弄清楚如何将整个数组/数据集转换为单独的变量,更不用说将其转换为单独的变量了"doSAtuff"功能.
my goal = parse a csv file into an array, and use that array in a few other operations. I can see that the file is getting parsed properly via the "console.log("Row:", row.data);", but I cannot figure out how to get that entire array/dataset into a separate variable, much less into the "doSAtuff" function.
function parseData(url, callBack) {
Papa.parse(url, {
download: true,
header: true,
dynamicTyping: true,
comments: "*=",
step: function(row) {
console.log("Row:", row.data);
},
complete: function(results) {
callBack(results.data);
}
});
}
function doStuff(data) {
//Data should be usable here, but is emtp
console.log("doStuff - console log '" + data + "' ?");
}
parseData(inputFile, doStuff);
我思考我想做类似的事情...
I think I want to do something like...
var csvArray = [];
csvArray = Papa.parse(url, {
download: true,
header: true,
dynamicTyping: true,
comments: "*
...
<some other stuff with csvArray>
但是现在我有点被车轴缠住了.
but i'm a bit wrapped around the axle at the moment.
推荐答案
我知道,如果删除header: true
设置,则Papa.parse将返回一个数组而不是对象.
I know that if you remove the header: true
setting then Papa.parse will return you an Array instead of an Object.
或者,您可以仅使用以下方法将返回的对象转换为数组:
Alternatively you could just convert the object that is returning into an array using something like:
function _arrayify(obj) { return Object.keys(obj).map(function (k) { return obj[k]; }) }
这篇关于如何将Papa.parse结果放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!