Untitled

 avatar
unknown
plain_text
6 months ago
1.4 kB
4
Indexable
function flattenTableData(columns, tableData) {
    return tableData.map(row => {
        const flatRow = {};
        columns.forEach(column => {
            if (row[column.key]) {
                // Extract the value based on the column type
                if (column.type === 'action' || column.type === 'cell-action') {
                    flatRow[column.key] = row[column.key].value.map(action => action.label).join(', ');
                } else if (column.html) {
                    flatRow[column.key] = row[column.key].value; // Assume this is already a flat string
                } else {
                    flatRow[column.key] = row[column.key].value;
                }
            } else {
                flatRow[column.key] = null; // or some default value if no data
            }
        });
        return flatRow;
    });
}

// Example usage
const columns = [
    { key: 'name', title: 'Name', type: 'text' },
    { key: 'age', title: 'Age', type: 'number' },
    { key: 'action', title: 'Actions', type: 'action' }
];

const tableData = [
    { name: { value: 'Alice' }, age: { value: 30 }, action: { value: [{ label: 'Edit' }, { label: 'Delete' }] } },
    { name: { value: 'Bob' }, age: { value: 25 }, action: { value: [{ label: 'Edit' }] } }
];

const flatData = flattenTableData(columns, tableData);
console.log(flatData);
Editor is loading...
Leave a Comment