Last active
December 24, 2020 08:05
-
-
Save KANE-99/e103b1197e2a02acc65d4a6d7d246179 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
Parser, | |
transforms: { flatten, unwind } | |
} = require("json2csv"); | |
try { | |
const parser = new Parser({ | |
fields: ["firstColumn", "b.secondColumn", "cars.0.brand", "cars.1.brand"], | |
transforms: [flatten({ arrays: true }), unwind({ paths: ["cars"] })] | |
}); | |
const csv = parser.parse({ | |
firstColumn: "first data", | |
b: { | |
secondColumn: "second data" | |
}, | |
cars: [{ brand: "AUDI" }, { brand: "TOYOTA" }] | |
}); | |
console.log(csv); | |
// OUTPUT: | |
// "firstColumn","b.secondColumn","cars.0.brand","cars.1.brand" | |
// "first data","second data","AUDI","TOYOTA" | |
} catch (err) { | |
console.log(err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment