let selectedFruit = "mango"
let fruitArray = [{fruit:"apple",locale:"US"},
{fruit:"orange",locale:"US"},
{fruit:"banana",locale:"US"},
{fruit:"apple",locale:"US"},
{fruit:"orange",locale:"IT"},
{fruit:"apple",locale:"MX"},
{fruit:"banana",locale:"FR"},
{fruit:"orange",locale:"IT"}
{fruit:"apple",locale:"MX"}]
Given the variable "selectedFruit" representing a fruit, I am looking to find if the fruit exists in the "fruitArray" with the same locale set to "US". If it does, I want to set a flag to true; otherwise, set it to false if the fruit exists with a different locale in the array.
for (let i = 0; i < fruitArray.length; i++) {
if (fruitArray[i]["fruit"].toString().toLowerCase() === selectedFruit.toString().toLowerCase() && fruitArray[i]["locale"] === "US") {
this.uniqueFruit = true;
break;
} else {
this.uniqueFruit = false;
}
}
The code above checks if the "selectedFruit" exists in the array of objects. However, the question remains on how to validate if the fruit exists in the array with the same locale set to "US".