Just beginning my coding journey with JavaScript and struggling to find a simple solution through online searches.
I'm attempting to locate a JavaScript function or similar method that can identify the name of a boolean in an array (not just its value) and provide it back to me. Additionally, these booleans must be stored in an array as I cannot use them as object keys for my current project requirements.
Below is a hypothetical scenario demonstrating my objective:
JavaScript:
function dinnerPreferences(){
var isChinese = false;
var isItalian = true;
var isIndian = false;
var isHomemade = true;
var isTakeout = false;
var dinnerOptions = [isChinese, isItalian, isIndian, isHomemade, isTakeout];
console.log("Dinner options values: " + dinnerOptions);
function getPreferences(){
var wantedDinnerOptions = [];
// Collecting true values in new array: wantedDinnerOptions
for(i = 0; i <=dinnerOptions.length; i++){
if(dinnerOptions[i]){
wantedDinnerOptions.push(dinnerOptions[i]);
}
}
// Accessing boolean values:
console.log("Confirming true values: " + wantedDinnerOptions);
// Finding the length of true values:
console.log("Length of true values: " + wantedDinnerOptions.length)
// Attempting to retrieve boolean NAMES(???):
console.log("Struggling to use Object.keys(). Any other ideas?");
}
getPreferences();
}
dinnerPreferences();