NPM酷庫048:json2csv,導出csv文件

NPM酷庫,天天兩分鐘,瞭解一個流行NPM庫。·git

在作數據管理類應用時,尤爲是管理後臺,常常須要將數據導出爲CSV文件,方便在Excel中離線打開查看。github

json2csv

json2csv是一款用於將JSON數據轉換成CSV格式文件的庫。json

const json2csv = require('json2csv');
const fs = require('fs');

const fields = ['car', 'price', 'color'];
const myCars = [
  {
    "car": "Audi",
    "price": 40000,
    "color": "blue"
  }, {
    "car": "BMW",
    "price": 35000,
    "color": "black"
  }, {
    "car": "Porsche",
    "price": 60000,
    "color": "green"
  }
];

let csv = json2csv({ data: myCars, fields: fields });
 
fs.writeFile('file.csv', csv, function(err) {
  if (err) throw err;
  console.log('file saved');
});

參考資料

https://github.com/zemirco/js...ui

相關文章
相關標籤/搜索