langleyfoxall / langleyfoxall/react-dynamic-data-table
Possible optimizations
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 12
- Forks
- 16
- PR merge metrics
- No merged PRs in 30d
Description
I've noticed that in DynamicDataTable.jsx getFields there is a lot of for loops. I feel that this can be written nicer by using pre-existing JavaScript functions such as Array.prototype.forEach. I think the logic can also be simplified.
Here's what I've come up with:
rows.forEach(row => {
Object.keys(row).forEach(rowFieldName => {
fields.forEach(field => {
if(field.name === rowFieldName) {
const label = rowFieldName.replace(new RegExp('_', 'g'), ' ').trim();
fields.push({
name: rowFieldName,
label,
});
}
})
})
})
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const rowFields = Object.keys(row);
for (let j = 0; j < rowFields.length; j++) {
const rowFieldName = rowFields[j];
let exists = false;
for (let k = 0; k < fields.length; k++) {
const field = fields[k];
if (field.name === rowFieldName) {
exists = true;
break;
}
}
if (!exists) {
const label = rowFieldName.replace(new RegExp('_', 'g'), ' ').trim();
fields.push({
name: rowFieldName,
label,
});
}
}
}
I personally think that we should be pushing to use existing JavaScript functions as much as possible which'll make code more compact and in my opinion easier to read.
Thoughts?
cc @DivineOmega @wheatleyjj
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/DynamicDataTable.jsx at getFields and compare the existing nested loops with the proposed array-method approach. The work is done when the logic is simpler while preserving the current field discovery, duplicate handling, and generated labels.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- frontend
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100