DEV Community

Discussion on: What is the standard way to keep UI state and backend state synced during updates? (React and Node)

Collapse
jesseayegba profile image
JesseAyegba

Amazing insight. The only problem I have with storing data in an object is the inability to loop through the items in that object. Using methods like "map" becomes practically impossible for a react developer.

Collapse
owenconti profile image
Owen Conti 🇨🇦

You can get around the loop issue by converting the object to an array before/during render:

Object.values(state.items).map(item => ...)
Enter fullscreen mode Exit fullscreen mode
Collapse
jamesjulius profile image
james-julius • Edited on

How about writing a custom function? I think something like this would work and would create a nice mini-optimisation to use objects as you're suggesting!

function mapObject(obj, callback) {
for (let [idx, val] in obj.entries()) {
callback(val, idx);
}
}