前提・実現したいこと
javascript
1function (a: string, b: string) { 2 if (a === "文字列") { 3 if (b === "文字列") { 4 // return 文字列 5 } 6 if (b === "文字列") { 7 // return 文字列 8 } 9 if (b === "文字列") { 10 // return 文字列 11 } 12 ・ 13 ・ 14 ・ 15 } 16 if (a === "文字列") { 17 ・ 18 ・ 19 ・ 20 } 21 ・ 22 ・ 23 ・ 24}
上記のような処理をもっとスマートに書きたいのですがよい方法はありますか?
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答3件
0
ベストアンサー
なんとなく、ロケール的なデータなのかなと思いました。
こういうデータは階層構造をもって管理することが多いです。
↓はRailsのロケール情報を定義するYAML
ja: datetime: distance_in_words: about_x_hours: one: 約1時間 other: 約%{count}時間 about_x_months: one: 約1ヶ月 other: 約%{count}ヶ月 about_x_years: one: 約1年 other: 約%{count}年 almost_x_years: one: 1年弱 other: "%{count}年弱" half_a_minute: 30秒前後 less_than_x_minutes: one: 1分以内 other: "%{count}分未満" less_than_x_seconds: one: 1秒以内 other: "%{count}秒未満" over_x_years: one: 1年以上 other: "%{count}年以上" x_days: one: 1日 other: "%{count}日" x_minutes: one: 1分 other: "%{count}分" x_months: one: 1ヶ月 other: "%{count}ヶ月" x_seconds: one: 1秒 other: "%{count}秒"
Objectにネストして持たせる(おすすめ)
type A = "a" | "b" | "c" | "d"; type B = "1" | "2" | "3" | "4"; const mapping: { [k1: string]: { [k2: string]: string } } = { "a": { "1": "AAAA", "2": "BBBB", "3": "CCCC" }, "b": { "1": "DDDD", "2": "EEEE", "3": "FFFF" } }; function hoge(a: A, b: B) { const x = mapping[a]; if(!x) return undefined; return x[b]; } console.info(hoge("a", "1")); // AAAA console.info(hoge("b", "1")); // DDDD console.info(hoge("X", "1")); // ERROR Argument of type '"X"' is not assignable to parameter of type 'A'.
Objectにネストしないで持たせる
type A = "a" | "b" | "c" | "d"; type B = "1" | "2" | "3" | "4"; const mapping: { [k: string]: string } = { "a_1": "AAAA", "a_2": "BBBB", ..... }; function hoge(a: A, b: B): string | undefined { return mapping[`${a}_${b}`]; } console.info(hoge("a", "1")); // AAAA console.info(hoge("b", "1")); // DDDD console.info(hoge("X", "1")); // ERROR Argument of type '"X"' is not assignable to parameter of type 'A'.
キーを配列(TypeScriptでは実現不可能)にしたObjectに持たせる
const mapping = { [["a", "1"]]: "AAAA", [["a", "2"]]: "BBBB", ..... };
投稿2018/08/15 11:15
退会済みユーザー
総合スコア0
0
次の if ... else ... の処理
if (a == ...) { return ... } else if (a == ...) { if (b == ...) { return ... } else if (b == ...){ return ... } else { return ... } } else if (a == ...) { ... }
を連想記憶で実装してみました。(typescript でなく , javascript でかいてます)
javascript
1var rules = { 2 "0": { 3 "0": "A0_B0", 4 "1": "A0_B1" 5 }, 6 "1": { 7 "0": "A1_B0", 8 "1": "A1_B1" 9 }, 10 "a": "A" 11}; 12function decision(args) { 13 if (args == undefined || 14 args.a === undefined || 15 rules[args.a] === undefined) { 16 return; 17 } 18 if (typeof (rules[args.a]) === 'string') { 19 return rules[args.a]; 20 } 21 return rules[args.a][args.b]; 22} 23console.log(decision({ a: "0", b: "0" })); 24console.log(decision({ a: "1", b: "1" })); 25console.log(decision({ a: "1", b: "x" })); 26console.log(decision({ a: "a" })); 27console.log(decision({})); 28console.log(decision());
if (b === ...) {return ...;} に相当する decision({ b: "b" }) は、上のコードでは実現できていません。
その処理が必要な場合は、連想記憶の key 自体を {a: "xxx", b: "yyy"} にして、 decision() を実装するようにすれば良いです。
投稿2018/08/15 13:43
総合スコア22324
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
a,b の組み合わせなら return する文字列を配列で定義して使うといいですよ。
投稿2018/08/15 10:16
退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/15 11:48
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/15 11:20