【问题标题】:reducing a nested object in javascript减少javascript中的嵌套对象
【发布时间】:2020-09-18 00:42:59
【问题描述】:

我有一个具有结构的对象

"Office": {
    "California" : {
        "Contract1": {
            1: {Price: 1000, Count: 5},
            2: {Price: 2000, Count: 11},
            3: {Price: 3000, Count: 3},
            4: {Price: 2000, Count: 1},...
        },
        "Contract2": {
            1: {Price: 7000, Count: 6},
            2: {Price: 1000, Count: 4},
            3: {Price: 67000, Count: 6},
            4: {Price: 500, Count: 2},...
        },
        "Contract3": {
            1: {Price: 4000, Count: 4},
            2: {Price: 4000, Count: 1},
            3: {Price: 5000, Count: 12},
            4: {Price: 5000, Count: 2},...
        }
    },
    "North Carolina" : {
        "Contract1": {
            1: {Price: 7000, Count: 4},
            2: {Price: 7000, Count: 4},
            3: {Price: 7000, Count: 4},
            4: {Price: 7000, Count: 4},...
        },
        "Contract2": {
            1: {Price: 6000, Count: 4},
            2: {Price: 2000, Count: 10},
            3: {Price: 3000, Count: 3},
            4: {Price: 2000, Count: 3},...
        },
        "Contract3": {
            1: {Price: 4000, Count: 5},
            2: {Price: 2000, Count: 4},
            3: {Price: 4000, Count: 3},
            4: {Price: 2000, Count: 4},...
        },...
}

我想把它简化为:

"Office": {
    "California" : {
        "Contract1": {
            1: {Price: 6000, Count: 19},
            2: {Price: 2000, Count: 1},...
        },
        "Contract2": {
            1: {Price: 75000, Count: 16},
            2: {Price: 500, Count: 2},...
        },
        "Contract3": {
            1: {Price: 13000, Count: 17},
            2: {Price: 5000, Count: 2},...
        }
    },
    "North Carolina" : {
        "Contract1": {
            1: {Price: 21000, Count: 12},
            2: {Price: 7, Count: 4},...
        },
        "Contract2": {
            1: {Price: 11000, Count: 17},
            2: {Price: 2000, Count: 3},...
        },
        "Contract3": {
            1: {Price: 10000, Count: 12},
            2: {Price: 2000, Count: 4},...
        },...
}

第一个结构上每个合同下的数字对象代表过去的月份。在第二个结构中,我想总结前 3 个数字对象的属性可以称之为 1。那么第二个 3 将是 2。这些是四分之一。

我想我可以通过 map、reduce 和 filter 的某种组合来做到这一点,但这些是数组函数,而且我有一个对象,所以我不知道该怎么做。我需要为非 ES6 解决方案执行此操作。

非常感谢任何指导。

【问题讨论】:

  • 到目前为止你尝试过什么?你遇到了什么问题?有什么错误吗?有for...in...for...of...Object.keys()Object.values()Object.entries().map().reduce()、...

标签: javascript dictionary object reduce


【解决方案1】:

您可以使用我编写的适用于对象的特殊 map 函数来做到这一点

const { pipe, fork, map, reduce, get } = rubico

const data = { "Office": {
    "California" : {
        "Contract1": {
            1: {Price: 1000, Count: 5}, 2: {Price: 2000, Count: 11},
            3: {Price: 3000, Count: 3}, 4: {Price: 2000, Count: 1},
            5: {Price: 1000, Count: 5}, 6: {Price: 2000, Count: 11},
            7: {Price: 3000, Count: 3}, 8: {Price: 2000, Count: 1},
            9: {Price: 1000, Count: 5}, 10: {Price: 2000, Count: 11},
            11: {Price: 3000, Count: 3}, 12: {Price: 2000, Count: 1},
        },
        "Contract2": {
            1: {Price: 7000, Count: 6}, 2: {Price: 1000, Count: 4},
            3: {Price: 67000, Count: 6}, 4: {Price: 500, Count: 2},
            5: {Price: 7000, Count: 6}, 6: {Price: 1000, Count: 4},
            7: {Price: 67000, Count: 6}, 8: {Price: 500, Count: 2},
            9: {Price: 7000, Count: 6}, 10: {Price: 1000, Count: 4},
            11: {Price: 67000, Count: 6}, 12: {Price: 500, Count: 2},
        },
        "Contract3": {
            1: {Price: 4000, Count: 4}, 2: {Price: 4000, Count: 1},
            3: {Price: 5000, Count: 12}, 4: {Price: 5000, Count: 2},
            5: {Price: 4000, Count: 4}, 6: {Price: 4000, Count: 1},
            7: {Price: 5000, Count: 12}, 8: {Price: 5000, Count: 2},
            9: {Price: 4000, Count: 4}, 10: {Price: 4000, Count: 1},
            11: {Price: 5000, Count: 12}, 12: {Price: 5000, Count: 2},
        }
    },
    "North Carolina" : {
        "Contract1": {
            1: {Price: 7000, Count: 4}, 2: {Price: 7000, Count: 4},
            3: {Price: 7000, Count: 4}, 4: {Price: 7000, Count: 4},
            5: {Price: 7000, Count: 4}, 6: {Price: 7000, Count: 4},
            7: {Price: 7000, Count: 4}, 8: {Price: 7000, Count: 4},
            9: {Price: 7000, Count: 4}, 10: {Price: 7000, Count: 4},
            11: {Price: 7000, Count: 4}, 12: {Price: 7000, Count: 4},
        },
        "Contract2": {
            1: {Price: 6000, Count: 4}, 2: {Price: 2000, Count: 10},
            3: {Price: 3000, Count: 3}, 4: {Price: 2000, Count: 3},
            5: {Price: 6000, Count: 4}, 6: {Price: 2000, Count: 10},
            7: {Price: 3000, Count: 3}, 8: {Price: 2000, Count: 3},
            9: {Price: 6000, Count: 4}, 10: {Price: 2000, Count: 10},
            11: {Price: 3000, Count: 3}, 12: {Price: 2000, Count: 3},
        },
        "Contract3": {
            1: {Price: 4000, Count: 5}, 2: {Price: 2000, Count: 4},
            3: {Price: 4000, Count: 3}, 4: {Price: 2000, Count: 4},
            5: {Price: 4000, Count: 5}, 6: {Price: 2000, Count: 4},
            7: {Price: 4000, Count: 3}, 8: {Price: 2000, Count: 4},
            9: {Price: 4000, Count: 5}, 10: {Price: 2000, Count: 4},
            11: {Price: 4000, Count: 3}, 12: {Price: 2000, Count: 4},
        },
    },
} }

const addContracts = (total, contract) => ({
  Price: total.Price + contract.Price,
  Count: total.Count + contract.Count,
})

const monthNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

const sumMonthsToQuarters = contract => {
  const quartered = {
    1: { Price: 0, Count: 0 },
    2: { Price: 0, Count: 0 },
    3: { Price: 0, Count: 0 },
    4: { Price: 0, Count: 0 },
  }
  for (const monthNum of monthNumbers) {
    const quarterNum = Math.ceil(monthNum / 3)
    quartered[quarterNum] = addContracts(quartered[quarterNum], contract[monthNum])
  }
  return quartered
}

const x = map(map(map(sumMonthsToQuarters)))(data)

console.log(JSON.stringify(x))
<script src="https://unpkg.com/rubico/index.js" crossorigin></script>

【讨论】:

    【解决方案2】:

    它会根据计数动态填充您所需的输出

      const data = { "Office": {
    "California" : {
        "Contract1": {
            1: {Price: 1000, Count: 5}, 2: {Price: 2000, Count: 11},
            3: {Price: 3000, Count: 3}, 4: {Price: 2000, Count: 1},
            5: {Price: 1000, Count: 5}, 6: {Price: 2000, Count: 11},
            7: {Price: 3000, Count: 3}, 8: {Price: 2000, Count: 1},
            9: {Price: 1000, Count: 5}, 10: {Price: 2000, Count: 11},
            11: {Price: 3000, Count: 3}, 12: {Price: 2000, Count: 1},
        },
        "Contract2": {
            1: {Price: 7000, Count: 6}, 2: {Price: 1000, Count: 4},
            3: {Price: 67000, Count: 6}, 4: {Price: 500, Count: 2},
            5: {Price: 7000, Count: 6}, 6: {Price: 1000, Count: 4},
            7: {Price: 67000, Count: 6}, 8: {Price: 500, Count: 2},
            9: {Price: 7000, Count: 6}, 10: {Price: 1000, Count: 4},
            11: {Price: 67000, Count: 6}, 12: {Price: 500, Count: 2},
        },
        "Contract3": {
            1: {Price: 4000, Count: 4}, 2: {Price: 4000, Count: 1},
            3: {Price: 5000, Count: 12}, 4: {Price: 5000, Count: 2},
            5: {Price: 4000, Count: 4}, 6: {Price: 4000, Count: 1},
            7: {Price: 5000, Count: 12}, 8: {Price: 5000, Count: 2},
            9: {Price: 4000, Count: 4}, 10: {Price: 4000, Count: 1},
            11: {Price: 5000, Count: 12}, 12: {Price: 5000, Count: 2},
        }
    },
    "North Carolina" : {
        "Contract1": {
            1: {Price: 7000, Count: 4}, 2: {Price: 7000, Count: 4},
            3: {Price: 7000, Count: 4}, 4: {Price: 7000, Count: 4},
            5: {Price: 7000, Count: 4}, 6: {Price: 7000, Count: 4},
            7: {Price: 7000, Count: 4}, 8: {Price: 7000, Count: 4},
            9: {Price: 7000, Count: 4}, 10: {Price: 7000, Count: 4},
            11: {Price: 7000, Count: 4}, 12: {Price: 7000, Count: 4},
        },
        "Contract2": {
            1: {Price: 6000, Count: 4}, 2: {Price: 2000, Count: 10},
            3: {Price: 3000, Count: 3}, 4: {Price: 2000, Count: 3},
            5: {Price: 6000, Count: 4}, 6: {Price: 2000, Count: 10},
            7: {Price: 3000, Count: 3}, 8: {Price: 2000, Count: 3},
            9: {Price: 6000, Count: 4}, 10: {Price: 2000, Count: 10},
            11: {Price: 3000, Count: 3}, 12: {Price: 2000, Count: 3},
        },
        "Contract3": {
            1: {Price: 4000, Count: 5}, 2: {Price: 2000, Count: 4},
            3: {Price: 4000, Count: 3}, 4: {Price: 2000, Count: 4},
            5: {Price: 4000, Count: 5}, 6: {Price: 2000, Count: 4},
            7: {Price: 4000, Count: 3}, 8: {Price: 2000, Count: 4},
            9: {Price: 4000, Count: 5}, 10: {Price: 2000, Count: 4},
            11: {Price: 4000, Count: 3}, 12: {Price: 2000, Count: 4},
        },
    },
    } }
    
    let count = 3;
    for(var parent in data['Office']){
      let child1 = data['Office'][parent];
      for(var con in child1){
        child1[con] = Object.keys(child1[con]).reduce((acc,ele,index)=>{
            let key = Math.trunc(index / count);
           return {...acc,...{[key+1]: (key+1 in acc) ? {Price:acc[key+1]['Price']+child1[con][ele]['Price'],Count:acc[key+1]['Count']+child1[con][ele]['Count']} : child1[con][ele]}}
        },{})
      }
    }
    console.log(data)

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 2019-10-16
      相关资源
      最近更新 更多