私はStackflowとGoogleにぶつかって解決策を見つけようとしていたのですが、ここに数時間滞在した後、ようやくアイデアを求めています。
これは私の配列です:
endangered = '#FFA500';
shutdown = '#FF0000';
active = '#00BB00';
// Build state array
var state = {};
state = {
NV: {
status: shutdown,
name: 'Las Vegas Charter School for the Deaf',
SchoolLink: 'http://www.lvcsd.org',
SourceLink: 'http://www.lvrj.com/news/charter-school-for-deaf-signs-off-in-bankruptcy-141399423.html',
ClosureDate: 'March 5, 2012',
Comment: 'Closure due to bankruptcy. State also adopted exclusive mainstreaming approach.'
},
WY: {
status: shutdown,
name: 'Wyoming School for the Deaf',
SchoolLink: 'http://www.wyomingdeaf.com/',
SourceLink: 'http://trib.com/news/local/article_94be7523-5bc5-5031-97ee-9431a205cfe9.html',
ClosureDate: '2000',
Comment: 'School replaced by a mainstream school. State also adopted exclusive mainstreaming approach.'
}
}
この時点でアクセスすると、次のようになります。
stateCode = 'NV';
currentColor = state[stateCode].status;
状態配列をチェックし、独自の配列を持つ「NV」配列を検索し、最後にステータスを検索します。ステータスには、そのステータスに関連付けられた色を参照する独自の変数もあります。この場合、シャットダウンのために「#FF0000」を返します。
そのようなコードを実行すると、「未定義」と表示されなくなります。しかし、私がこのようにすると:
currentColor = state['NV'].status;
その後、完全に機能します。しかし、これは静的になるため、目的を無効にします。関数からのフィードバックに基づいており、常に変化するため、stateCodeを動的に保つことができる必要があります。
私はこのようにそれを行うことができます:
if(stateCode === 'NV') currentColor = state['NV'].status;
if(stateCode === 'WY') currentColor = state['WY'].status;
しかし、それはすぐに肥大化するでしょう。これを処理するためのより良い方法が必要です。何か案は?