I have a data structure like this:
LAYOUT = {
'Picking Type 1': {
1: {
1: {
1: 'SKU1',
2: 'SKU2',
//etc
},
//etc
},
//etc
},
//etc
}
It corresponds to products in bins on shelves, so the keys are "Area": "Rack": "Level": "Bin": "Sku"
Trouble is, when a user updates one AreaRackLevelBin to a Sku, I need to find any other location that has that sku and remove it (a sku can only be on one place).
The naive way to do this is:
for (let area in layout)
for (rack in layout[area])
for (level in layout[area][rack])
for (bin in layout[area][rack][level])
if (layout[area][rack][level][bin] === sku)
layout[area][rack][level][bin] = "";
// call whatever code updates the backend
But this is obviously nested for looping. I did some back of the envelope math and calculated there will only ever be about 2,250 bins to traverse (5 areas, 10 racks, 9 levels, 5 bins).
Is there a way to traverse the deepest layer of a deeply nested object without 4 layers of for loops? Heck, I guess I'm not even sure if there's a performance hit when I would absolutely need to iterate over all 2,250 bins to check anyway. One thought I had was an array of pointers to those levels, but JavaScript doesn't have pointers. Any ideas?
UPDATE: The problem is not that I have to type LAYOUT[A][B][C][D], it's that I have to iterate all the keys in nested order to find the one value. This isn't even necessarily a problem if there isn't a more time efficient method.
My thought is that I could store an array of all the deepest objects and just iterate that. I imagine that would be a reference, not a value. It just doesn't seem explicit enough for me to trust I'm not modifying the values.