fix: refs #7905 added comments to flatten.js

This commit is contained in:
Guillermo Bonet 2024-09-05 09:21:00 +02:00
parent d49707828b
commit 2aeff19578
2 changed files with 15 additions and 24 deletions

View File

@ -1,7 +1,22 @@
/**
* Flattens an array of objects by converting each object into a flat structure.
*
* @param {Array} dataArray Array of objects to be flattened
* @return {Array} Array of flattened objects
*/
function flatten(dataArray) {
return dataArray.map(item => flattenObj(item.__data));
}
/**
* Recursively flattens an object, converting nested properties into a single level object
* with keys representing the original nested structure.
*
* @param {Object} data The object to be flattened
* @param {String} [prefix=''] Optional prefix for nested keys
* @return {Object} Flattened object
*/
function flattenObj(data, prefix = '') {
let result = {};
try {

View File

@ -40,27 +40,3 @@ module.exports = Self => {
return [toCSV(dataFlatted), 'text/csv', `inline; filename="buys-${id}.csv"`];
};
};
// function flattenJSON(dataArray) {
// return dataArray.map(item => flatten(item.__data));
// }
// function flatten(data, prefix = '') {
// let result = {};
// try {
// for (let key in data) {
// if (!data[key]) continue;
// const newKey = prefix ? `${prefix}_${key}` : key;
// const value = data[key];
// if (typeof value === 'object' && value !== null && !Array.isArray(value))
// Object.assign(result, flatten(value.__data, newKey));
// else
// result[newKey] = value;
// }
// } catch (error) {
// console.error(error);
// }
// return result;
// }