0

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.

kuldeep
  • 11
  • 2
  • what `arrOfMenuKeys[i] == arrOfMenuValues[i];` is supposed to do ? – AbbasEbadian Jan 25 '21 at 08:31
  • @AbbasEbadian, using arrOfMenuKeys[i] == arrOfMenuValues[i]; I'm displaying the name of the key(s) which is equivalent to value(s). I hope I'm clear with my answer. – kuldeep Jan 25 '21 at 17:41

2 Answers2

0

You can create a new array which will hold already written prices then in every iterate, check if current price is present in that array or not.

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;

    function searchUsingPriceRangeFromMenu(mainObject, minimumPrice, maximumPrice) {
    for (let key in mainObject) {
        let arrOfPriceRangeKeys = [];
        if (typeof mainObject[key] !== "object" && mainObject[key] >= minimumPrice && mainObject[key] <= maximumPrice && !printedPrices.includes(mainObject[key])) {
            //pay attention to last part of condition
            printedPrices.push(mainObject[key]); // push the new price
            arrOfPriceRangeKeys = key;
            document.write(arrOfPriceRangeKeys + "<br>")
        } else {
            if (typeof mainObject[key] === "object") {
                searchUsingPriceRangeFromMenu(mainObject[key], minimumPrice, maximumPrice, arrOfPriceRangeKeys)
            }

        }
    } return;
}
let printedPrices = []; //new array
searchUsingPriceRangeFromMenu(menu, minimumPrice, maximumPrice);
AbbasEbadian
  • 653
  • 5
  • 15
  • @AbbasEbadian-Your answer to my question is really helpful and it worked. If you are available, could you please help me again ASAP when I changed my object to a nested object. Thanks in advance. – kuldeep Jan 27 '21 at 16:28
  • Thank you again for your support. @AbbasEbadian – kuldeep Jan 28 '21 at 16:10
  • please go through the link below and help me again only If you are free. My mean is not to bother you again and again but as a beginner I find your answers very much understandable. @AbbasEbadian https://stackoverflow.com/q/65940905/15069520 – kuldeep Jan 31 '21 at 07:54
0

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>

Besides that, don't use document.write. If you use it after the page has finished loading it'll mess up your page.

Thomas
  • 11,958
  • 1
  • 14
  • 23