lundi 25 janvier 2021

How to get only the 1st appearing key when having multiple duplicate values in JavaScript

I have the following JavaScript object:

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

let minimumPrice = 400;
let maximumPrice = 600;

I need to show Pizzas between the given price range but want to show the Pizza only once with a price of 490 that is coming first. I got the price range results but are with all Pizzas having a price is 490. Here's my code below:

function searchUsingPriceRange(minimumPrice, maximumPrice) {
    let arrOfMenuKeys = [];
    let arrOfMenuValues = [];
    arrOfMenuKeys = Object.keys(menu)
    arrOfMenuValues = Object.values(menu)
    for (let i = 0; i < arrOfMenuValues.length; i++) {
        if (arrOfMenuValues[i] >= minimumPrice && arrOfMenuValues[i] <= maximumPrice) {
            arrOfMenuKeys[i] == arrOfMenuValues[i];
            document.write(arrOfMenuKeys[i] + "<br>")
        }
    } return;
}
searchUsingPriceRange(minimumPrice, maximumPrice);

My Results:

capsicumPizza
onionPizza
pepperoniPizza
mushroomsPizza
extraCheesePizza
blackOlivesPizza
greenPeppersPizza

I know my result is okay with what I have done in my code. But after that, I stuck off and didn't get what to do. All I want to end up with as following:

capsicumPizza
onionPizza
pepperoniPizza
extraCheesePizza
blackOlivesPizza

Where should I put filtration in my code to get what I'm looking for is. Somebody help, please.

Aucun commentaire:

Enregistrer un commentaire