前提・実現したいこと
初歩的な質問で失礼します。
typescriptで都道府県のオブジェクトを参照してくるだけの簡単な物を作っているのですが、どうやったら理想通りに参照できるかがわからず困っています。
1、{Kanto.tokyo.name} で「東京」と表示させたい。
2、理想はmixin等を使って自由に取り出せるようになる事です。
該当のソースコード
Typescript
1interface PrefsType { 2 id : string, 3 name : string, 4 area : string 5} 6 7type AreasType = any 8 9const tokyo : PrefsType = { 10 id : 'tokyo', 11 name : '東京', 12 area : 'Kanto' 13} 14 15const ibaraki : PrefsType = { 16 id : 'ibaraki', 17 name : '茨城', 18 area : 'Kanto' 19} 20 21const chiba : PrefsType = { 22 id : 'chiba', 23 name : '千葉', 24 area : 'Kanto' 25} 26 27const saitama : PrefsType = { 28 id : 'saitama', 29 name : '埼玉', 30 area : 'Kanto' 31} 32 33const tochigi : PrefsType = { 34 id : 'tochigi', 35 name : '栃木', 36 area : 'Kanto' 37} 38 39const gunma : PrefsType = { 40 id : 'gunma', 41 name : '群馬', 42 area : 'Kanto' 43} 44 45export const Kanto: AreasType = [ 46 {tokyo}, 47 {ibaraki}, 48 {chiba}, 49 {saitama}, 50 {tochigi}, 51 {gunma} 52]; 53 54console.log(Kanto.tokyo.name) //コンソールに何も表示されず 55 56 57試しに以下の形にすると一応コンソールに表示されるようにはなるのですが、{...tokyo.tokyo...}とidが二つ並ぶのはもう少し何とかしたいと思っています。 58export const Kanto: AreasType = { 59 tokyo : {tokyo}, 60 ibaraki:{ibaraki}, 61 chiba:{chiba}, 62 saitama:{saitama}, 63 tochigi:{tochigi}, 64 gunma:{gunma}, 65}; 66console.log(Kanto.tokyo.name) //undefined 67console.log(Kanto.tokyo.tokyo.name) //東京 68
数時間かけて調べたりしてもどうすれば良いか分からなかったので、教えて頂けたら幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/17 16:06
2020/01/20 00:38