I manage to find min and max in an array of object, but in the same time I have to include other variables. I have extra special_min_price and special_max_price, how can I include them in reduce 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
});
I assumed you wanted the special prices to be a part of that reduce. If that's the case, then you could try adding a step before your reduce where you add on to the list of prices. I imagine it would 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With