Closed gilles-crealp closed 6 years ago
Shouldn't be the same if I don't define fields
and parse multiple different data? So I can define the options (except fields
) only once.
let Json2csvParser = require('json2csv').Parser
let json2csv = new Json2csvParser({
header: false
})
console.log(json2csv.parse([
{
"a": 1,
"b": 2
},
{
"a": 3,
"b": 4
}
]))
// Output:
// 1,2
// 3,4
console.log(json2csv.parse([
{
"a": 1,
"x": 2
},
{
"a": 3,
"x": 4
}
]))
// Output:
// 1,
// 3,
// Should be?
// 1,2
// 3,4
It's a bug indeed!! I'll fix it asap. Thanks for reporting with such a clear code sample :)
Not so good: I didn't figure out that the problem was when you use header: false
only. ;)
I've change the sample code.
And you need to have the same object for the options!
Work:
let Json2csvParser = require('json2csv').Parser
let json2csv = new Json2csvParser({
header: false
})
console.log(json2csv.parse([
{
'a': 1,
'b': 2
},
{
'a': 3,
'b': 4
}
]))
// Output:
// 1,2
// 3,4
console.log()
json2csv = new Json2csvParser({
header: false
})
console.log(json2csv.parse([
{
'a': 1,
'x': 2
},
{
'a': 3,
'x': 4
}
]))
// Output:
// 1,2
// 3,4
Do not work:
let Json2csvParser = require('json2csv').Parser
let json2csvParserOptions = {
header: false
}
let json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
{
'a': 1,
'b': 2
},
{
'a': 3,
'b': 4
}
]))
// Output:
// 1,2
// 3,4
console.log()
json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
{
'a': 1,
'x': 2
},
{
'a': 3,
'x': 4
}
]))
// Output:
// 1,
// 3,
Yeah. The problem is that running any parsing modified the options object. Can't believe that no one noticed until now... (including myself).
Once again, thanks for reporting. Will be fixed whenever the PR get's merged.
Fix published in v4.2.1
Thx for the reactivity!
Thanks for the detailed issue 💃
Versions: