javascript - vue js中的嵌套数组总计

标签 javascript arrays vue.js

This the result of my query I want to get the total of deduction

3: {total_ot: 0, total_days: 96, total_allowance: 0, wrk_id: 3, f_name: "JOHN", l_name: "DOE",…}
daily_rate: 560
date: {2020-09-24: {work_hours: 8, adj_hour: 0}, 2020-09-25: {work_hours: 8, adj_hour: 0},…}
deduction: {1: {amount: 700}, 2: {amount: 700}}
f_name: "JOHN"
l_name: "DOE"
m_name: null
total_allowance: 0
total_days: 96
total_ot: 0
wrk_id: 3

In my computed

   deducted(){
  const deducted  = Object.values(this.workersSummaryData)
  return  deducted.reduce((acc, item) =>{      
  console.log(item.deducted)    
  if(item.deduction)
  return acc + item.deduction;
  else return acc

      }, 0)
   }

 },

最佳答案

尝试使用reduce()方法并获取 amount 属性对其求和:

const sum = Object.values(obj.deduction)
    .reduce((a, {amount}) => {return a + amount }, 0);

一个例子:

let obj = {
    total_ot: 0, total_days: 96, total_allowance: 0, wrk_id: 3, f_name: "JOHN", l_name: "DOE",
    daily_rate: 560,
    date: { '2020-09-24': { work_hours: 8, adj_hour: 0 }, '2020-09-25': { work_hours: 8, adj_hour: 0 } },
    deduction: { 1: { amount: 700 }, 2: { amount: 700 } },
    f_name: "JOHN",
    l_name: "DOE",
    m_name: null,
    total_allowance: 0,
    total_days: 96,
    total_ot: 0,
    wrk_id: 3
};

const sum = Object.values(obj.deduction)
    .reduce((a, { amount }) => { return a + amount }, 0);
console.log(sum);

关于javascript - vue js中的嵌套数组总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64311522/

相关文章:

javascript - Vue 多选不删除选项

javascript - SVG def 元素是否可编辑

Java 数组列表 : check if an id is in the list

Java - 读入7个整数,计算重复出现的次数

c++ - 在 C++ 中访问二维数组的起始元素

javascript - Vue.js 中未定义组件 props

javascript - 如何使用 Material UI 网格组件将一个项目左对齐,另一项右对齐

javascript - 仅当子菜单处于事件状态时才跟随超链接

Javascript 在 for 循环中按日期分组

javascript - CORS策略阻止前端到后端的请求