I have the following JavaScript nested object:
let menu = {
vegetarian: {
plainPizza: 100,
redChilliPizza: 150,
cheesePizza: 200,
capsicumPizza: 160,
onionPizza: 200
},
nonVegetarian: {
supremeMeatPizza: 100,
meatPizza: 130,
meatLoversPizza: 160,
chickenPizza: 200,
chilliMeatPizza: 200
}
};
let minimumPrice = 100;
let maximumPrice = 200;
I need to show Pizzas between the given price range but want to show the Pizza only that is coming 1st and having price same for other pizzas. For example for a Pizza of Rs. 100 I just want to show only 'plainPizza', not others in my results. And same for others too. I got the price range results but are with all Pizza. Here's my code below:
function searchUsingPriceRangeFromMenu(mainObject, minimumPrice, maximumPrice) {
for (let key in mainObject) {
let arrOfPriceRangeKeys = [];
if (typeof mainObject[key] !== "object" && mainObject[key] >= minimumPrice && mainObject[key] <= maximumPrice) {
arrOfPriceRangeKeys = key;
document.write(arrOfPriceRangeKeys + "<br>")
} else {
if (typeof mainObject[key] === "object") {
searchUsingPriceRangeFromMenu(mainObject[key], minimumPrice, maximumPrice, arrOfPriceRangeKeys)
}
}
} return;
}
searchUsingPriceRangeFromMenu(menu, minimumPrice, maximumPrice);
My Results:
plainPizza
redChilliPizza
cheesePizza
capsicumPizza
onionPizza
supremeMeatPizza
meatPizza
meatLoversPizza
chickenPizza
chilliMeatPizza
So please tell me what I need to do is and where? All I want to end up with as following:
plainPizza
redChilliPizza
cheesePizza
capsicumPizza
meatPizza
help me.
question from:
https://stackoverflow.com/questions/65881157/how-to-get-only-the-1st-appearing-key-when-having-multiple-duplicate-values-in-n 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…