Отфильтровать по условиям можно так:
var sell = [
{"breed": "sosna", "size": "20x100x6000", "cost": 1500},
{"breed": "sosna", "size": "30x100x6000", "cost": 2500},
{"breed": "sosna", "size": "40x100x6000", "cost": 3500},
{"breed": "sosna", "size": "20x100x6000", "cost": 4500},
{"breed": "sosna", "size": "30x100x6000", "cost": 5500},
{"breed": "sosna", "size": "40x100x6000", "cost": 6500},
];
var breed = ["sosna"];
var size = ["30x100x6000", "20x100x6000"];
var params = { breed, size };
var activeKeys = Object.keys(params).filter(function(key) {
return params[key].length;
});
var filtered = sell.filter(function(item) {
return activeKeys.every(function(key) {
return params[key].indexOf(item[key]) != -1;
});
});
С использованием
ES6:
const sell = [
{"breed": "sosna", "size": "20x100x6000", "cost": 1500},
{"breed": "sosna", "size": "30x100x6000", "cost": 2500},
{"breed": "sosna", "size": "40x100x6000", "cost": 3500},
{"breed": "sosna", "size": "20x100x6000", "cost": 4500},
{"breed": "sosna", "size": "30x100x6000", "cost": 5500},
{"breed": "sosna", "size": "40x100x6000", "cost": 6500},
];
const breed = ["sosna"];
const size = ["30x100x6000", "20x100x6000"];
const params = { breed, size };
const activeKeys = Object.keys(params).filter(key => params[key].length);
const filtered =
sell.filter(item => activeKeys.every(key => params[key].includes(item[key])));
Найти минимум можно так:
var min = sell.reduce(function(acc, el) {
return acc < el.cost ? acc : el.cost;
}, Infinity);
или:
var values = sell.map(function(el) {
return el.cost;
});
var min = Math.min.apply(Math, values);
С использованием
ES6:
const min = sell.reduce((acc, el) => acc < el.cost ? acc : el.cost, Infinity);
или:
const min = Math.min(...sell.map(el => el.cost));