如何解决 json2csv header 和数据问题?节点

how can I solve json2csv header and data problem? Node js

我阅读了 json2csv 文档并将其应用到我的程序中

const {parse} = require('json2csv')

 header = [customerNo,Name,BranchCode,representive,overall]
 list = [{cNo:1,name:Jack,bCode:3,rps:Bill,overall},{cNo:2,name:Justin,bCode:2,rps:Wade,overall:180}]

    const fields = header;
    const opts = { fields };
    try {
        var csv = parse(list,opts);
          csv=csv.replace(/,/g,";")
        
     
          console.log(csv);
      } catch (err) {
        console.log(err)
         
      

  
}

result = customerNo;Name;BranchCode;representative;overall

我的数据不属于 headers。为什么我有这个问题。我做错了什么吗?

由于您想要的列名与 属性 键不同,您需要将它们定义为自定义:

const fields = [{
        label: 'customerNo',
        value: 'cNo'
    }, {
        label: 'Name',
        value: 'name'
    }, {
        label: 'BranchCode',
        value: 'bCode'
    }, {
        label: 'representive',
        value: 'rps'
    }, {
        label: 'overall',
        value: 'overall'
    }
];

这在文档中也有解释 -> https://github.com/zemirco/json2csv#example-3