Suppose I'm iterating over data and building up a multi-dimensional array. Inside the loop I want to do:
arr[x][y].push(z);
But if it's the first time a particular value has been seen for x or y then the array doesn't exist yet.
Is there an idiom in JS for doing this?
EDIT: Edited to make it clear this is about arrays, not objects.
cakeandingredientare numbers, you likely want to be using an object forarrthan an array...((arr ??= [])[cake] ??= [])[ingredient] = quantity;(or if they're not numeric indices,((arr ??= {})[cake] ??= {})[ingredient] = quantity;)