爸爸解析返回数据数组,其中所有内容都转换为字符串

时间:2018-07-06 05:40:01

标签: javascript reactjs papaparse

我正在使用Papa.parse将.CSV解析为React,我得到的输出是这样的:

[
  {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错字< / strong>)

或者是否有任何配置可以阻止Papa.parse这样做?

3 个答案:

答案 0 :(得分:2)

您可以使用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);

答案 1 :(得分:2)

使用 isNaN 来检查数字,然后返回以解析该数字字符串,否则按原样返回。

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);

答案 2 :(得分:1)

启用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>