I need to create an array of objects for items greater than zero. I have the following code:
const payments = [];
if (values.ops > 0) {
payments.push({
"tax": options.find(item => item.value === "ops"),
"type": ops.find(item => item.value === "tax"),
"year": values.year,
"period": ops.find(item => item.type === "period"),
"sum": values.ops
})
};
if (values.ops1 > 0) {
payments.push({
"tax": options.find(item => item.value === "ops1"),
"type": ops1.find(item => item.value === "tax"),
"year": values.year,
"period": ops1.find(item => item.type === "period"),
"sum": values.ops1
})
};
if (values.oms > 0) {
payments.push({
"tax": options.find(item => item.value === "oms"),
"type": oms.find(item => item.value === "tax"),
"year": values.year,
"period": oms.find(item => item.type === "period"),
"sum": values.oms
})
};
Is there any way to simplify this, so that I could check if an item is greater than 0 and make payments.push only once?
valuesvariable? Thanks!