1

i read the json2csv documentation and applied it in my program

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;representive;overall

My data not coming under headers. Why I have this problem. Am I doing something wrong ?

4

1 回答 1

0

Since your desired column names are different than the property keys you need to define them as custom:

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

This is also explained in the docs -> https://github.com/zemirco/json2csv#example-3

于 2020-07-02T18:09:50.703 回答