如何将CSV读取到React Native State Array中? [英] How to read CSV into React Native State Array?

查看:48
本文介绍了如何将CSV读取到React Native State Array中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:如何将CSV中的数据加载到React中的数组中-特别是在this.state中作为对象.

I have a question re: how to load data from CSV into an array in React -- specifically inside the this.state as an object.

让我们从这里开始,状态只是硬编码的值.

Lets say I start with this, where the state is just hardcoded values.

export default class App extends Component<{}> {
  constructor(props) {
    super(props);
    this.state = {
      cards: [
        {itemname: 'Item 1',
          restoname: 'Resto 1',
          url: 'https://res.cloudinary.com/grubhub/image/upload/v1515191165/bkf4niv9okipbc8y6o8h.jpg',
          description: 'Desc 1'},
        {itemname: 'Item 2',
          restoname: 'Resto 2',
          url: 'https://res.cloudinary.com/grubhub/image/upload/v1514060352/twhriy3hvbzayktrjp91.jpg',
          description: 'Desc 2'}
      ]
    };
  }

我是否可以通过这种方式从数据中读取数据而不是从CSV中读取数据?

Is there a way for me to read in that data instead from a CSV where the data is like this?

谢谢大家!

推荐答案

尝试使用此npm软件包.这将帮助您将 csv 转换为 json ,您可以轻松地在react组件中使用它-

Try this npm package. This will help you convert csv to json which you can easily use in your react component - https://www.npmjs.com/package/csvtojson

import csv from 'csvtojson';
const csvFilePath='<path to csv file>';
csv()
.fromFile(csvFilePath)
.on('json',(jsonObj)=>{
    // combine csv header row and csv line to a json object
    // jsonObj.a ==> 1 or 4
})
.on('done',(error)=>{
    console.log('end')
})

这篇关于如何将CSV读取到React Native State Array中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆