I'm working with a function that is designed to retrieve specific descriptions for objects nested within an array. The purpose of the function (findSettings()) is to take in an array (systemSettings) and a key (tab12) as arguments, then use a switch statement to match the key and provide its corresponding description.
In simpler terms, if the argument passed to the function is 'tab12', it should output 'Description for tab12'.
I initially attempted to locate a matching object using the find method, which was successful. However, when I tried implementing the switch statement, I encountered an error message stating 'Object is possibly undefined'.
const systemSettings = [
{key: 'tab1', value: 'Main Tab'},
{key: 'tab12', value: 'Tab 12'},
{key: 'tab13', value: 'Tab 13'},
{key: 'tab4', value: 'Tab 4'}
]
type sampObj = {
key: string;
value: string;
}
let info: string = '';
function findSetting(arr: sampObj[], settingKey: string) {
const selectedObjs = arr.find(obj => obj.key === settingKey);
switch(selectedObjs.key) {
case 'tab1':
info += 'Description for tab1';
break;
case 'tab12':
info += 'Description for tab12';
break;
case 'tab13':
info += 'Description for tab13';
break;
case 'tab4':
info += 'Description for tab4';
break;
default:
info += 'No description available'
}
}
findSetting(systemSettings, 'tab12')