简体   繁体   English

为大于零的项目创建一个对象数组

[英]Create an array of objects for items greater than zero

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?有什么方法可以简化这一点,以便我可以检查项目是否大于 0 并只进行一次payments.push

You could create a mapper object using Shorthand property names .您可以使用速记属性名称创建mapper对象。 This will map "ops" key with ops array as value and so on.这会将"ops"键与ops数组映射为值等等。 Then loop through the entries of this object and push to the payments array based on values[key]然后循环遍历此对象的entries并根据values[key]推送到payments数组

const mapper = {
  ops,
  ops1,
  oms
}

const payments = []

for (const [key, array] of Object.entries(mapper)) {
  if (values[key] > 0)
    payments.push({
      "tax": options.find(item => item.value === key),
      "type": array.find(item => item.value === "tax"),
      "year": values.year,
      "period": array.find(item => item.type === "period"),
      "sum": values[key]
    })
}
const payments = [];

[
  "ops", 
  "ops1", 
  "oms"
].forEach(prop=>{
    if(values[props]>0){
        payments;.push({
            tax: options.find(item => item.value === prop),
            type: ops.find(item => item.value === "tax"),
            year: values.year,
            period: ops.find(item => item.type === "period"),
            sum: values[prop]
        })
    }
})

You could take an object for ops , ops1 and oms and iterate the keys and oush new objects if necessary.您可以为opsops1oms获取一个对象,并在必要时迭代键和 oush 新对象。

data = { ops, ops1, oms }

Object.keys(data).forEach(k => {
    if (values[k] <= 0) return;
    payments.push({
        tax: options.find(item => item.value === k),
        type: data[k].find(item => item.value === "tax"),
        year: values.year,
        period: data[k].find(item => item.type === "period"),
        sum: values[k]
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 OF 元素中的组长度大于零,则检查对象数组 - Check in object array if the length of groups is greater than zero in a OF element Javascript,对大于零的数值数组进行排序 - Javascript, sorting a numerical array above greater than zero 使用对象数组创建一个新数组,其中包含原始对象数组中数值大于 15 的所有对象 - Using array of object create a new array that contains all the objects from the original array of object that have a numerical value greater than 15 创建对象数组的列表项 - Create list items for array of objects 如何构造项目数组以创建对象数组 - How to structure array of items to create an array of objects 当数据大于0(零)时,Ajax Jquery / JavaScript创建HTML元素 - Ajax Jquery / JavaScript create HTML Elements when data is greater than 0 (zero) 当计数器变量大于数组长度时,将计数器变量重新启动为零的函数 - 不起作用 - Function to restart a counter variable at zero when it is greater than an array's length -- not working 如果大于零,则在对象中减去方法 - Subtract method in object if greater than zero 000,000,000.00是否大于零? - Is 000,000,000.00 greater than zero? 检查数字是否大于零(0)的不同方法 - Different ways in checking if a number is greater than zero (0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM