Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

if statement - How to get only the 1st appearing key when having multiple duplicate values in nested object - JavaScript

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can filter the list of keys from the menu to be not only in the given price range, but also find the first key for the current price.

let menu = {
  capsicumPizza: 450,
  onionPizza: 530,
  pepperoniPizza: 490,
  mushroomsPizza: 490,
  extraCheesePizza: 600,
  blackOlivesPizza: 580,
  greenPeppersPizza: 490
};
let minimumPrice = 400;
let maximumPrice = 600;

function searchUsingPriceRange(minimumPrice, maximumPrice) {
  return Object.keys(menu)
    .filter((key, _, keys) => {
      const price = menu[key];
      // item is in price range
      return price >= minimumPrice &&
        price <= maximumPrice &&
        // it's the first item with that price
        key === keys.find(item => menu[item] === price)
    })
}

document.querySelector("#container").innerHTML = searchUsingPriceRange(minimumPrice, maximumPrice).join("<br>")
<div id="container"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...