node.js - How to write into a csv with multiple rows and columns -
now have several json in mongodb, such as:
{ title: "title_1", content: "content_1", author: "author_1" } and want write these data csv file format below:
title content author title_1 content_1 author_1 title_2 content_2 author_2 ... i used node-csv-parser module. write in first column in csv file, such as:
title content author title_1,content_1,author_1 title_1,content_1,author_2 ... what should achieve aim? please show me examples. appreciated!
convert nested arrays, join.
var array = [ { title: "title_1", content: "content_1", author: "author_1" }, { title: "title_1", content: "content_1", author: "author_2" } ]; var keys = object.keys(array[0]); var csv = [keys.join('\t')]; array.foreach(function (data) { var row = []; keys.foreach(function (key) { row.push(data[key]); }); csv.push(row.join('\t')); }); csv = csv.join('\n'); output:
title content author title_1 content_1 author_1 title_1 content_1 author_2
Comments
Post a Comment