我设法在对象数组中找到min和max,但同时我必须包含其他变量。我有额外的special_min_price和special_max_price,如何在reduce方法中包含它们?
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
});
答案 0 :(得分:1)
我认为你希望特价能成为减价的一部分。如果是这种情况,那么您可以尝试在缩减之前添加一个步骤,然后添加到价格列表中。我想它看起来像这样:
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();