0

I've been wondering about this for a while, I'm using and setting complex objects in a Redux Store state, and the way I do it is this (proc is an array and mod is the value to set):

if(proc.length > 4) state[proc[0]][proc[1]][proc[2]][proc[3]][proc[4]] = mod
else if(proc.length > 3) state[proc[0]][proc[1]][proc[2]][proc[3]] = mod
else if(proc.length > 2) state[proc[0]][proc[1]][proc[2]] = mod
else if(proc.length > 1) state[proc[0]][proc[1]] = mod
else state[proc[0]] = mod

This allows me to modify an object value with up to 5 levels of nesting, but as you might imagine this code repeats all over the place and it's ugly. I find it hard to believe this is the correct way of doing this, but I have no idea how to search for a better solution. I'm looking for something more like:

state[proc] = mod
  • Sounds like [`XY Problem to me`](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is your goal here? Why do you need such data structure? – PM 77-1 Feb 05 '21 at 19:22
  • Sorry if I didn't explain myself correctly, I have organized data in some big objects with many keys everywhere, those keys have objects as value, somethimes nesting up to 3 or 4 times. I also have a method like this: adjustState(["events", event, "location", "country"], "France"). So I intend to change the object value: events > [event name] > location > country. That's what my code does – Franco Boragno Feb 05 '21 at 20:08

0 Answers0