let foo = {};
const key1 = 'a';
const key2 = 'b';
const key3 = 'c';
foo[key1][key2][key3] = [1, 2];
When I trying to do something similar I get:
Uncaught TypeError: Cannot read property 'b' of undefined
You have to create the nested object before you can create a property in it.
let foo = {}
const key1 = 'a'
const key2 = 'b'
const key3 = 'c'
foo[key1] = {};
foo[key1][key2] = {};
foo[key1][key2][key3] = [1, 2];
console.log(foo);
If the list of keys is generated dynamically in an array, see Populate nested object from array? for a function to create all the objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With