0
votes

I have this situation. I have an array of objects, something like: {

    "products":[
        {
            "name": "Black T-Shirt",
            "id": "black-tshirt",
            "price": 123,
            "description": "Good materials"
        },{
            "name": "White T-Shirt",
            "id": "white-tshirt",
            "price": 123,
            "description": "Good materials"
        },{
            "name": "Red T-Shirt",
            "id": "red-tshirt",
            "price": 123,
            "description": "Good materials"
        },{
            "name": "Green T-Shirt",
            "id": "green-tshirt",
            "price": 250,
            "description": "Good materials"
        },{
            "name": "Yellow T-Shirt",
            "id": "yellow-tshirt",
            "price": 120,
            "description": "Good materials"
        },{
            "name": "Brown T-Shirt",
            "id": "brown-tshirt",
            "price": 250,
            "description": "Good materials"
        }
    ]
}

Now I could have an array of values based on the price like

let prices: number[] = [123, 250];

What I need is to retrieve all the results with price 123 and 250. I can use filter function to have one of these 2 results like:

const results = products.filter((product) => product.price === 123);

but I could have more of one because of the array of prices. I need to sum all values. Something like:

const results = products.filter((product) => product.price === 123 && 250 );

What's the best solution? Can be the include function?

const results = products.filter((product) => prices.include(product.price));

Thanks

Did you test your approach? Did something not work as expected? - David
your last option looks best - cmgchess
Someone told me that it wasn't the best approach in terms of performances. Is it correct? I don't know any other way actually. I though was the best one - Atlas91