如何在Javascript中为无效输入设置消息? [英] How to set Message for an Invalid Input in Javascript?
本文介绍了如何在Javascript中为无效输入设置消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象数组.
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";
以下是该功能,工作正常.
I want to Show the whole items that Match Gender name. My input is let searchForGender = "Men";
Below is the function and working fine.
function getClothesAvaGen() {
mainMenu.forEach(obj => {
for (let i = 0; i < obj["gender"].length; i++) {
if (obj["gender"][i] === searchForGender && searchForType === "" && searchForSize === "" && searchForBrand === ""
&& searchForStock === "" && searchForPrice === "" && searchForRating === "") {
console.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>")
}
}
});
}
getClothesAvaGen();
输出:
gender : Men
type : Shirt
size : Small
brand : Zara
price : 300
stock : 10
rating : 4
gender : Men
type : Shirt
size : Medium
brand : Nike
price : 600
stock : 20
rating : 5
gender : Men
type : Shirt
size : Large
brand : Adidas
price : 700
stock : 30
rating : 5
gender : Men
type : tShirt
size : Medium
brand : Nike
price : 400
stock : 50
rating : 4
gender : Men
type : Jeans
size : Small
brand : USPA
price : 2000
stock : 30
rating : 4
但是我想为诸如"Men1","MenAbc"之类的输入设置未找到匹配项"
.或任何其他无效输入.在上面编写的代码中,如何以及在何处进行设置.对于错误的输入,找不到匹配项"
应该只出现一次.
But I want to set 'No Match Found'
for input like "Men1", "MenAbc" or for any other invalid input. How and Where can I set this in my above written code. 'No Match Found'
should appear only once for a wrong input.
推荐答案
您可以使用 array.filter
和 array.includes
函数来简化此操作.
You can use array.filter
and array.includes
functions to make it easier.
function getClothesAvaGen(){
// 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";
function getClothesAvaGen(){
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();
这篇关于如何在Javascript中为无效输入设置消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文