Papa parse returns 全部转成字符串的数据数组
Papa parse returns Data array in which everything is converted to string
我正在使用 React
将 .CSV 解析为 Papa.parse
,我得到的输出是这样的:
[
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
]
问题是 Papa.parse 将所有内容都隐藏到双引号下的 string
!
我们如何将它们转换回 number
? (如果它们是原始形式的数字!问题是某些 Field
可能没有 number
类型,例如位置或地址或名称:它们是 string
打字错误)
或者有什么配置可以防止Papa.parse
这样做吗?
您可以使用 map()
将数组项转换为数字:
var papa =[
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
];
papa = papa.map(item => {
item.Year = Number(item.Year);
item.BMW = Number(item.BMW);
item.Toyots = Number(item.Toyots);
item.Mercedes = Number(item.Mercedes);
return item;
});
console.log(papa);
您可能也对以下方法感兴趣:
var papa = [
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881", Status : "Test"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
];
papa = papa.map(item => {
Object.keys(item).forEach(k => item[k] = isNaN(item[k])? item[k] : Number(item[k]));
return item;
});
console.log(papa);
使用 isNaN 检查数字,如果是,则 return 解析该数字字符串,否则 return 按原样。
var papa =[
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881","location":"pune"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
];
papa = papa.map(item => {
const keys = Object.keys(item);
let newObj={}
keys.map((m)=>{
newObj[m] = isNaN(item[m]) ? item[m] : parseInt(item[m])
})
return newObj
});
console.log(papa);
启用 dynamicTyping
选项以保留号码:
const csv = "Year,BMW,Toyota,Mercedes,Location\n1929,1896,9547,4881,Germany"
const csvData = Papa.parse(csv, {header:true, dynamicTyping: true}).data
console.log(typeof csvData[0].Year);
console.log(typeof csvData[0].Location);
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.5.0/papaparse.min.js">
</script>
我正在使用 React
将 .CSV 解析为 Papa.parse
,我得到的输出是这样的:
[
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
]
问题是 Papa.parse 将所有内容都隐藏到双引号下的 string
!
我们如何将它们转换回 number
? (如果它们是原始形式的数字!问题是某些 Field
可能没有 number
类型,例如位置或地址或名称:它们是 string
打字错误)
或者有什么配置可以防止Papa.parse
这样做吗?
您可以使用 map()
将数组项转换为数字:
var papa =[
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
];
papa = papa.map(item => {
item.Year = Number(item.Year);
item.BMW = Number(item.BMW);
item.Toyots = Number(item.Toyots);
item.Mercedes = Number(item.Mercedes);
return item;
});
console.log(papa);
您可能也对以下方法感兴趣:
var papa = [
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881", Status : "Test"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
];
papa = papa.map(item => {
Object.keys(item).forEach(k => item[k] = isNaN(item[k])? item[k] : Number(item[k]));
return item;
});
console.log(papa);
使用 isNaN 检查数字,如果是,则 return 解析该数字字符串,否则 return 按原样。
var papa =[
{Year: "1929", BMW: "1896", Toyots: "9547", Mercedes: "4881","location":"pune"},
{Year: "1930", BMW: "6548", Toyots: "4741", Mercedes: "8096"},
{Year: "1931", BMW: "5013", Toyots: "6269", Mercedes: "3908"},
{Year: "1932", BMW: "2468", Toyots: "9858", Mercedes: "1623"},
{Year: "1933", BMW: "3364", Toyots: "5595", Mercedes: "8638"},
{Year: "1934", BMW: "2032", Toyots: "2570", Mercedes: "8041"},
{Year: "1935", BMW: "3579", Toyots: "6886", Mercedes: "6938"},
{Year: "1936", BMW: "2865", Toyots: "3336", Mercedes: "1996"}
];
papa = papa.map(item => {
const keys = Object.keys(item);
let newObj={}
keys.map((m)=>{
newObj[m] = isNaN(item[m]) ? item[m] : parseInt(item[m])
})
return newObj
});
console.log(papa);
启用 dynamicTyping
选项以保留号码:
const csv = "Year,BMW,Toyota,Mercedes,Location\n1929,1896,9547,4881,Germany"
const csvData = Papa.parse(csv, {header:true, dynamicTyping: true}).data
console.log(typeof csvData[0].Year);
console.log(typeof csvData[0].Location);
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.5.0/papaparse.min.js">
</script>