Find min and max using shorthand but with additional condition

I manage to find min and max in the object array, but at the same time I need to include other variables. I have extra special_min_price and special_max_price, how can I include them in the reduction method?

const price = [
{min_price:100, max_price:200},
{min_price:70, max_price:200},
{min_price:50, max_price:100},
{min_price:100, max_price:400},
{min_price:120, max_price:600},
],
special_min_price = 40,
special_max_price = 800;

let x = _.reduce(price, function(a, b) {
            a.min_price = Math.min(a.min_price, b.min_price);
            a.max_price = Math.max(a.max_price, b.max_price);
            return a
        });

      

+3


source to share


1 answer


I assumed you want special prices to be part of this cut. If so then you can try adding a step before the cut where you add to the price list. I assume it will look something like this:



const price = [
{min_price:100, max_price:200},
{min_price:70, max_price:200},
{min_price:50, max_price:100},
{min_price:100, max_price:400},
{min_price:120, max_price:600},
],
special_min_price = 40,
special_max_price = 800;

let x = _.chain(price)
         .concat({min_price:special_min_price,
             max_price:special_max_price})
         .reduce(function(a, b) {
             a.min_price = Math.min(a.min_price, b.min_price);
             a.max_price = Math.max(a.max_price, b.max_price);
             return a
         })
         .value();

      

+1


source







All Articles