英文:
How to gracefully initialize a series of objects in TypeScript?
问题
我需要在我的代码中临时创建一个对象,格式类似于 one.two.three.four
。
但是我知道的初始化方式很繁琐。有没有办法可以用少量代码来优雅地初始化它们?
我现在正在做的很丑陋:
let one: any;
one = {};
one.two = {};
one.two.three = {};
console.log(one.two.three);
英文:
I need to temporarily create an object in format like one.two.three.four
in my code.
But the way I known to initialize them is annoying. Is there anyway I can gracefully initialize them with few lines of code?
What I am doing now is ugly:
let one: any;
one = {};
one.two = {};
one.two.three = {};
console.log(one.two.three);
答案1
得分: 1
如克里斯建议的那样,我们可以简单地这样做:
let one: any = {two: {three: {four:false}}}
英文:
As Chris suggested, we can simply do it by:
let one: any = {two: {three: {four:false}}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论