Skip to content

Commit

Permalink
Change small details as requested
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardo.rodrigues committed Jul 10, 2017
1 parent 976d04f commit 2c1b159
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
69 changes: 34 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,46 +327,45 @@ var fs = require('fs');
var fields = ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'];
var myCars = [
{
"carModel": "BMW",
"price": 15000,
"items": [
"carModel": "BMW",
"price": 15000,
"items": [
{
"name": "airbag",
"color": "white"
}, {
"name": "dashboard",
"color": "black"
}
]
}, {
"carModel": "Porsche",
"price": 30000,
"items": [
{
"name": "airbag",
"items": [
{
"name": "airbag",
"color": "white"
"position": "left",
"color": "white"
}, {
"name": "dashboard",
"color": "black"
"position": "right",
"color": "gray"
}
]
}, {
"carModel": "Porsche",
"price": 30000,
"items": [
]
}, {
"name": "dashboard",
"items": [
{
"name": "airbag",
"items": [
{
"position": "left",
"color": "white"
}, {
"position": "right",
"color": "gray"
}
]
},
{
"name": "dashboard",
"items": [
{
"position": "left",
"color": "gray"
}, {
"position": "right",
"color": "black"
}
]
"position": "left",
"color": "gray"
}, {
"position": "right",
"color": "black"
}
]
]
}
]
}
];
var csv = json2csv({ data: myCars, fields: fields, unwindPath: ['items', 'items.items'] });
Expand Down
8 changes: 4 additions & 4 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function checkParams(params) {
return Object.keys(item);
});

dataFields = lodashFlatten(dataFields);//_.flattenDeep(params.data);//
dataFields = lodashFlatten(dataFields);
params.fields = lodashUniq(dataFields);
}

Expand Down Expand Up @@ -294,10 +294,8 @@ function createColumnContent(params, str) {
* @returns {Array} Array of objects containing all rows after unwind of chosen paths
*/
function createDataRows(originalData, unwindPaths) {
var dataRows = originalData;
var dataRows = [];
if (unwindPaths.length) {
dataRows = [];

originalData.forEach(function(dataElement) {
var dataRow = [dataElement];

Expand All @@ -307,6 +305,8 @@ function createDataRows(originalData, unwindPaths) {

Array.prototype.push.apply(dataRows, dataRow);
});
} else {
dataRows = originalData;
}

return dataRows;
Expand Down

0 comments on commit 2c1b159

Please sign in to comment.