✅ – bfrisco – 14-14 Dec 18

Is there any shorthand way to do this?
const obj = {};
obj['a']['b']['c'] = 'a'
// Cannot read properties of undefined (reading 'b')
const obj = {};
obj['a']['b']['c'] = 'a'
// Cannot read properties of undefined (reading 'b')
Current solution:
const obj = {}
obj['a'] = {}
obj['a']['b'] = {};
obj['a']['b']['c'] = 'a';
const obj = {}
obj['a'] = {}
obj['a']['b'] = {};
obj['a']['b']['c'] = 'a';
E
EvokeMe485d ago
const obj = {a: {b: {c: `a`}}}
const obj = {a: {b: {c: `a`}}}
@bfrisco ^^
B
Brice485d ago
ah, thanks. i'd still want to preserve any other values, but can use optional chaining with destructuring i believe
E
EvokeMe485d ago
Also u can use dynamic keys, like this:
const key = `b`
const obj = {a: {[key]: {c: `a`}}}
const key = `b`
const obj = {a: {[key]: {c: `a`}}}
@bfrisco ^^
B
Brice485d ago
yeah, my actual code is like this now:
stats = {
...stats,
[deviceType]: {
...stats?.[deviceType],
// etc
}
}
stats = {
...stats,
[deviceType]: {
...stats?.[deviceType],
// etc
}
}
I think this will work, TY
UU
Unknown User485d ago