New to Lodash..like today new. I found a post here that was pretty simple to understand for filtering an object by its keys. SO Post
So what I'm trying to do is similar but I want the value. My app provides a user with multiple options for concocting a drink. At the bare minimum they must select one option. When they don't select an option I want to alert them that they must so a basic if/else is what I was going for.
My Object:
var pref = {
'strong': $('#strong').find(':selected').val() === 'Yes',
'salty': $('#salty').find(':selected').val() === 'Yes',
'bitter': $('#bitter').find(':selected').val() === 'Yes',
'sweet': $('#sweet').find(':selected').val() === 'Yes',
'fruity': $('#fruity').find(':selected').val() === 'Yes'
};
When the user selects nothing my value for every key will read false
.
My validation code at the moment:
function validateInputs(pref) {
var emptyFields = _.filter(pref, function(value, key) {
if (emptyFields.key === false) {
alert('You must select at least one option.');
} else {
// run program
randomDrink = myBartender.createDrink(pref);
}
});
}
So I want to be able to filter by the value saying:
if(all keys are false) {alert} else {run the program}
Is there a handler that will return the value of my key for LoDash? If you see any glaring errors with what I'm trying to do please let me know. I've never tried filtering an object like this so learning is in progress!