I've gone bonkers hitting Stackflow and Google trying to find solutions, and finally am asking for ideas after being at this for a couple of hours.
This is my array:
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.'
}
}
Accessing it then at this point would be something like:
stateCode = 'NV';
currentColor = state[stateCode].status;
It'd check the state array, look up the 'NV' array which has its own array, then finally look up the status, which also has its own variable, which references the color associated with that status. In this case, it'd be returning '#FF0000' for shutdown.
If I do the code like that, it fails saying 'undefined'. If I however do it like this:
currentColor = state['NV'].status;
It then works perfectly. But this defeats the purpose, as it becomes static. I need to be able to keep stateCode dynamic, as it's based on feedback from a function, and will always be changing.
I could do it like this:
if(stateCode === 'NV') currentColor = state['NV'].status;
if(stateCode === 'WY') currentColor = state['WY'].status;
But it'd quickly become bloated. There has to be a better way to handle this. Any ideas?