mardi 21 janvier 2020

More efficient way to check all conditionals from forEach response using javascript

I have a <select> sending the value of the option (listed below). The value is then passed into the React Hook. This works but is there a drier/cleaner/more efficient way to go through the conditional statements?

Select Options

const animals = [
  "Cows", "Pigs", "Sheep", "Goats", "Lambs", "Rabbits"
]

Function

const _updateLocationData = (value) => { 
    var tempLocations = [];
    locations.forEach(function(res) {
        if (value === "Cows" && res.Cows === "Yes") {
            tempLocations.push(res);
        }
        if (value === "Pigs" && res.Pigs === "Yes") {
            tempLocations.push(res);
        }
        if (value === "Sheep" && res.Sheep === "Yes") {
            tempLocations.push(res);
        }
        if (value === "Goats" && res.Goat === "Yes") {
            tempLocations.push(res);
        }
        if (value === "Lambs" && res.Lamb === "Yes") {
            tempLocations.push(res);
        }
        if (value === "Rabbits" && res.Rabbit === "Yes") {
            tempLocations.push(res);
        }
    });
}

Aucun commentaire:

Enregistrer un commentaire