Skip to content

Commit 5558270

Browse files
committed
handle formatting on nested objects
1 parent b3db8ed commit 5558270

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/components/csvExportButton.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ const flattenObject = (obj, parentKey = "") => {
1515
if (typeof obj[key] === "object" && obj[key] !== null && !Array.isArray(obj[key])) {
1616
Object.assign(flattened, flattenObject(obj[key], fullKey));
1717
} else if (Array.isArray(obj[key])) {
18-
obj[key].forEach((item, index) => {
19-
if (typeof item === "object" && item !== null) {
20-
Object.assign(flattened, flattenObject(item, `${fullKey}[${index}]`));
21-
} else {
22-
flattened[`${fullKey}[${index}]`] = item;
23-
}
24-
});
18+
// Handle arrays of objects by applying the formatter on each property
19+
flattened[fullKey] = obj[key]
20+
.map((item) =>
21+
typeof item === "object"
22+
? JSON.stringify(
23+
Object.fromEntries(
24+
Object.entries(flattenObject(item)).map(([k, v]) => [k, getCippFormatting(v, k, "text", false)])
25+
)
26+
)
27+
: getCippFormatting(item, fullKey, "text", false)
28+
)
29+
.join(", ");
2530
} else {
2631
flattened[fullKey] = obj[key];
2732
}
@@ -52,6 +57,7 @@ export const CSVExportButton = (props) => {
5257
const formattedRow = {};
5358
columnKeys.forEach((key) => {
5459
const value = row[key];
60+
// Pass flattened data to the formatter for CSV export
5561
formattedRow[key] = getCippFormatting(value, key, "text", false);
5662
});
5763
return formattedRow;

0 commit comments

Comments
 (0)