Skip to content Skip to sidebar Skip to footer

How To Set Message For An Invalid Input In Javascript?

I have an array of objects. let mainMenu = [ { brand: 'Zara', type: 'Shirt', gender: ['Men', 'Women', 'Boys', 'Girls'], size: 'Small', i

Solution 1:

Right after the for loop, like this

functiongetClothesAvaGen() {
    mainMenu.forEach(obj => {

        let isNotFound = truefor (let i = 0; i < obj["gender"].length; i++) {
            if (obj["gender"][i] === searchForGender && searchForType === "" && searchForSize === "" && searchForBrand === ""
                && searchForStock === "" && searchForPrice === "" && searchForRating === "") {
                isFound = falseconsole.log(obj)
                document.write("gender : " + obj["gender"][i] + "<br>" + "type : " + obj["type"] + "<br>" + "size : " + obj["size"] + "<br>" + "brand : " + obj["brand"] + "<br>" +
                    "price : " + obj["price"] + "<br>" + "stock : " + obj["stock"] + "<br>" + "rating : " + obj["rating"] + "<br>" + "<br>")
            }
        }

        if(isNotFound) {
           // do something
        }
    });
}
getClothesAvaGen();

Solution 2:

You can use array.filter and array.includes functions to make it easier.

functiongetClothesAvaGen(){
  // filter items those their gender, includes our searchForGender term.let availableItems = mainMenu.filter(item => item['gender'].includes(searchForGender));

  // if not found such items then print no match and get out of function.if (availableItems.length == 0){
    console.log('No Match');
    return;
  }

  //If found , for each of them, print some data
  availableItems.forEach(obj =>{
    console.log("gender : " + searchForGender + "\n" + "Brand : " + obj['brand'] + "\n" + "Other Data...")});
}

getClothesAvaGen();

let mainMenu = [
    {
        brand: "Zara",
        type: "Shirt",
        gender: ["Men", "Women", "Boys", "Girls"],
        size: "Small",
        image: "",
        description: "",
        price: "300",
        colour: "Red",
        stock: "10",
        discount: 5,
        rating: "4"
    },
    {
        brand: "Nike",
        type: "Shirt",
        gender: ["Men", "Women", "Boys"],
        size: "Medium",
        image: "",
        description: "",
        price: "600",
        colour: "Red",
        stock: "20",
        discount: 5,
        rating: "5"
    },
    {
        brand: "Adidas",
        type: "Shirt",
        gender: ["Men", "Women"],
        size: "Large",
        image: "",
        description: "",
        price: "700",
        colour: "Red",
        stock: "30",
        discount: 5,
        rating: "5"
    },
    {
        brand: "Puma",
        type: "tShirt",
        gender: ["Boys", "Girls"],
        size: "Small",
        image: "",
        description: "",
        price: "600",
        colour: "Red",
        stock: "40",
        discount: 5,
        rating: "5"
    },
    {
        brand: "Nike",
        type: "tShirt",
        gender: ["Men", "Women", "Girls"],
        size: "Medium",
        image: "",
        description: "",
        price: "400",
        colour: "Red",
        stock: "50",
        discount: 5,
        rating: "4"
    },
    {
        brand: "Zara",
        type: "tShirt",
        gender: ["Women", "Boys", "Girls"],
        size: "Large",
        image: "",
        description: "",
        price: "600",
        colour: "Red",
        stock: "40",
        discount: 5,
        rating: "5"
    },
    {
        brand: "USPA",
        type: "Jeans",
        gender: ["Men"],
        size: "Small",
        image: "",
        description: "",
        price: "2000",
        colour: "Red",
        stock: "30",
        discount: 5,
        rating: "4"
    },
    {
        brand: "Adidas",
        type: "Jeans",
        gender: ["Women"],
        size: "Medium",
        image: "",
        description: "",
        price: "2500",
        colour: "Red",
        stock: "20",
        discount: 5,
        rating: "5"
    },
    {
        brand: "Puma",
        type: "Jeans",
        gender: ["Boys"],
        size: "Large",
        image: "",
        description: "",
        price: "3000",
        colour: "Red",
        stock: "10",
        discount: 5,
        rating: "4"
    }
];
let searchForGender = "Men";
functiongetClothesAvaGen(){
  let availableItems = mainMenu.filter(item => item['gender'].includes(searchForGender));
  if (availableItems.length == 0){
    console.log('No Match');
    return;
  }
  availableItems.forEach(obj =>{
    console.log("gender : " + searchForGender + "\n" + "Brand : " + obj['brand'] + "\n" + "Other Data...")});
}

getClothesAvaGen();

Post a Comment for "How To Set Message For An Invalid Input In Javascript?"