我有一个嵌套的对象:
let menu = {
vegetarian: {
vegStarter: {
plainPizza: 100,
redChilliPizza: 150,
cheesePizza: 200,
capsicumPizza: 160,
onionPizza: 200,
},
vegMainCourse: {
pepperoniPizza: 150,
mushroomsPizza: 160,
extraCheesePizza: 250,
blackOlivesPizza: 220,
greenPeppersPizza: 180,
}
},
nonVegetarian: {
nonVegStarter: {
supremeMeatPizza: 100,
meatPizza: 130,
meatLoversPizza: 160,
chickenPizza: 200,
chilliMeatPizza: 200
},
nonVegMainCourse: {
butterMeatPizza: 220,
spicyChickenPizza: 170,
seafoodPizza: 300,
spinachEggPizza: 200,
eggPizza: 250,
}
}
}
这是我对一个函数的输入:
let getSearchTermtoFindByNameFromNonVegMainCourse = "spinachEggPizza";
函数如下:
function searchUsingNameFromNonVegMainCourseCategory(mainObject, getSearchTermtoFindByNameFromNonVegMainCourse) {
let arrOfNonVegMainCourseKeys = [];
Object.keys(mainObject).forEach(key => {
if (getSearchTermtoFindByNameFromNonVegMainCourse === ' ') {
alert("Enter Valid Value.")
} else {
if (key !== getSearchTermtoFindByNameFromNonVegMainCourse) {
} else {
if (key === getSearchTermtoFindByNameFromNonVegMainCourse && typeof mainObject[key] !== "object") {
arrOfNonVegMainCourseKeys = key;
document.write(arrOfNonVegMainCourseKeys + " : " + mainObject[key] + "<br>");
} else {
if (typeof mainObject[key] === "object") {
searchUsingNameFromNonVegMainCourseCategory(mainObject[key], getSearchTermtoFindByNameFromNonVegMainCourse, arrOfNonVegMainCourseKeys)
}
}
}
}
}); return document.write("No Match Found.");
}
searchUsingNameFromNonVegMainCourseCategory(menu.nonVegetarian.nonVegMainCourse, getSearchTermtoFindByNameFromNonVegMainCourse);
我想只在函数的输入不匹配时设置No Match Found.
。我的代码工作得很好,但对于一个匹配的输入,它也显示No Match Found.
的结果,我不想明显地显示。以下是上述代码的输出:
spinachEggPizza : 200
No Match Found.
但我只想显示spinachEggPizza : 200
作为输出。
我这是怎么了?
这是因为您总是返回"document.write("No Match Found.")"在函数块的底部,您可以设置在有条件的else块中打印该消息:
if (getSearchTermtoFindByNameFromNonVegMainCourse === ' ') {
alert("Enter Valid Value.")
} else {
if (key !== getSearchTermtoFindByNameFromNonVegMainCourse) {
} else {
if (key === getSearchTermtoFindByNameFromNonVegMainCourse && typeof mainObject[key] !== "object") {
arrOfNonVegMainCourseKeys = key;
document.write(arrOfNonVegMainCourseKeys + " : " + mainObject[key] + "<br>");
} else {
if (typeof mainObject[key] === "object") {
searchUsingNameFromNonVegMainCourseCategory(mainObject[key], getSearchTermtoFindByNameFromNonVegMainCourse, arrOfNonVegMainCourseKeys)
}
}
} else {
document.write("No Match Found.")
}
});
}
为什么不直接做:
const menu =
{ vegetarian :
{ vegStarter : { plainPizza : 100, redChilliPizza : 150, cheesePizza : 200, capsicumPizza : 160, onionPizza : 200 }
, vegMainCourse : { pepperoniPizza : 150, mushroomsPizza : 160, extraCheesePizza : 250, blackOlivesPizza : 220, greenPeppersPizza : 180 }
}
, nonVegetarian :
{ nonVegStarter : { supremeMeatPizza : 100, meatPizza : 130, meatLoversPizza : 160, chickenPizza : 200, chilliMeatPizza : 200 }
, nonVegMainCourse : { butterMeatPizza : 220, spicyChickenPizza : 170, seafoodPizza : 300, spinachEggPizza : 200, eggPizza : 250 }
} }
function foo(path, prop ) {
let res = null
try {
res = path[ prop ]
}
catch(e) {
alert("Enter Valid Value.")
return null
}
return (res==undefined) ? 'No Match Found.' : `${prop} : ${res}`
}
console.log( foo(menu.nonVegetarian.nonVegMainCourse, 'spinachEggPizza') )
.as-console-wrapper { max-height: 100% !important; top: 0; }
要遍历嵌套对象并使用递归查找一些特殊的东西,我们可以使用一个小技巧。
将一个空数组传递给函数,在函数内部,只要遇到要查找的键,就将其附加到数组中。
最后返回数组作为结果。
function searchPrice(mainObject, search_text, found_prices) {
Object.keys(mainObject).forEach(key => {
if (typeof mainObject[key] == "object"){
// Note that we pass found_prices here as well
searchPrice(mainObject[key], search_text, found_prices);
}else{
if (key === search_text) {
// if found , push price to found_prices
found_prices.push(mainObject[key]);
}
}
});
return found_prices
}
let menu = {
vegetarian: {
vegStarter: {
plainPizza: 100,
redChilliPizza: 150,
cheesePizza: 200,
capsicumPizza: 160,
onionPizza: 200,
},
vegMainCourse: {
pepperoniPizza: 150,
mushroomsPizza: 160,
extraCheesePizza: 250,
blackOlivesPizza: 220,
greenPeppersPizza: 180,
}
},
nonVegetarian: {
nonVegStarter: {
supremeMeatPizza: 100,
meatPizza: 130,
meatLoversPizza: 160,
chickenPizza: 200,
chilliMeatPizza: 200
},
nonVegMainCourse: {
butterMeatPizza: 220,
spicyChickenPizza: 170,
seafoodPizza: 300,
spinachEggPizza: 200,
eggPizza: 250,
}
}
}
let search_text = 'spinachEggPizza';
// Pass an empty array to function which will be filled with price or prices of matching keys
let prices = searchPrice(menu, search_text, []);
// If any prices were found then we print them out
if (prices.length > 0){
prices.forEach(price => {
document.write(search_text + " : " + price+ "<br>");
});
}else{
document.write("No Match.");
}