gpt4 book ai didi

javascript - 将 Papaparse 行转换为对象

转载 作者:太空宇宙 更新时间:2023-11-03 22:27:26 25 4
gpt4 key购买 nike

papaparse 是否支持返回由标题列作为键控的对象实例数组?

例如,我有一个如下所示的 CSV 文件:

sku, location, quantity
'sku1', 'Chicago', 3
'sku2', 'New York, 4

我希望 papaparse 返回的数组如下所示:

 [{sku: 'sku1', location: 'Chicago', quantity: 3}, ...]

这应该也是可能的:

results[0].sku == 'sku1'
results[1].quantity == 4

最佳答案

尝试将 header: true 添加到配置参数。

来自the docs :

header: If true, the first row of parsed data will be interpreted as field names. An array of field names will be returned in meta, and each row of data will be an object of values keyed by field name instead of a simple array. Rows with a different number of fields from the header row will produce an error. Warning: Duplicate field names will overwrite values in previous fields having the same name.

例如:

Papa.parse('./data.csv', {
download: true,
header: true, // gives us an array of objects
dynamicTyping: true,
complete: ({ data }) => console.log(data),
});

鉴于您的数据应该产生

[
{
sku: 'sku1',
location: 'Chicago',
quantity: 3,
},
{
sku: 'sku2',
location: 'New York',
quantity: 4,
},
]

关于javascript - 将 Papaparse 行转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43691714/

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