简体   繁体   English

如何计算对象的匹配唯一键数(javascript)

[英]How to count # of matching unique keys of an object (javascript)

I'm trying to get the unique keys of an object, add the values and count how many of the unique key there are of each, but can't figure out a graceful solution. 我正在尝试获取对象的唯一键,添加值并计算每个键的唯一键数量,但是无法找到一个合适的解决方案。

raw array of objs: objs的原始数组:

[{
    "2017W40": 15.25
}, {
    "2017W40": 16.5
}, {
    "2017W40": 16.6
}, {
    "2017W40": 11
}, {
    "2017W40": 17.857142857142858
}, {
    "2017W40": 13
}, {
    "2017W41": 19
}, {
    "2017W41": 20.25
}, {
    "2017W41": 18.25
}, {
    "2017W41": 13
}, {
    "2017W41": 20.333333333333332
}, {
    "2017W41": 20.5
}, {
    "2017W41": 20.5
}, {
    "2017W42": 19.5
}, {
    "2017W42": 19.5
}, {
    "2017W42": 20
}, {
    "2017W42": 19.5
}, {
    "2017W42": 19.333333333333332
}, {
    "2017W42": 20
}, {
    "2017W42": 20.5
}, {
    "2017W43": 20
}, {
    "2017W43": 20
}, {
    "2017W43": 19.666666666666668
}, {
    "2017W43": 19.6
}, {
    "2017W43": 19.666666666666668
}, {
    "2017W43": 19
}, {
    "2017W43": 19.5
}, {
    "2017W44": 21
}, {
    "2017W44": 19.5
}, {
    "2017W44": 20
}, {
    "2017W44": 19.5
}]

summed object: 求和对象:

{
    "2017W40": 90.20714285714286,
    "2017W41": 131.83333333333331,
    "2017W42": 138.33333333333331,
    "2017W43": 137.43333333333334,
    "2017W44": 80
}

But I'd also like to include a count of each (so I can get an average instead of just the sum) Here is the method I"m using: 但我也想包括每个的计数(这样我就可以获得平均值而不是总和)这是我正在使用的方法:

function getValueAvg(valsByKey) {
  const dateObj = {};
  for (let i = 0; i < valsByKey.length; ++i) {
    for (const obj in valsByKey[i]) {
      if (dateObj[obj]) {
        // add them if exists already
        dateObj[obj] = dateObj[obj] + valsByKey[i][obj];
      } else {
        // first
        dateObj[obj] = valsByKey[i][obj];
      }
    }
  }

So either a final object of 所以要么是

{
    {"2017W40": 90.20714285714286, count:x},
    {"2017W41": 131.83333333333331, count:x},
    {"2017W42": 138.33333333333331, count:x},
    {"2017W43": 137.43333333333334, count:x},
    {"2017W44": 80, count:x}
}

or just the value divided by count would work wonders for me. 或仅将价值除以计数对我来说就是奇迹。 "2017W40": 90.20714285714286/count “ 2017W40”:90.20714285714286 /计数

You can just create new objects that has two fields, a value or sum field, and a count field. 您可以只创建具有两个字段的新对象,即值或总和字段以及计数字段。 When you iterate over the data, you just add into the value field and then up the count by one. 遍历数据时,只需将其添加到value字段中,然后将计数加1。

 function getValueAvg(valsByKey) { const dateObj = {}; for (let i = 0; i < valsByKey.length; ++i) { for (const obj in valsByKey[i]) { if (dateObj[obj]) { // add them if exists already dateObj[obj].val = dateObj[obj].val + valsByKey[i][obj]; dateObj[obj].count += 1; } else { // first dateObj[obj] = {"val":valsByKey[i][obj], "count":1}; } } } return dateObj; } var data = [{ "2017W40": 15.25 }, { "2017W40": 16.5 }, { "2017W40": 16.6 }, { "2017W40": 11 }, { "2017W40": 17.857142857142858 }, { "2017W40": 13 }, { "2017W41": 19 }, { "2017W41": 20.25 }, { "2017W41": 18.25 }, { "2017W41": 13 }, { "2017W41": 20.333333333333332 }, { "2017W41": 20.5 }, { "2017W41": 20.5 }, { "2017W42": 19.5 }, { "2017W42": 19.5 }, { "2017W42": 20 }, { "2017W42": 19.5 }, { "2017W42": 19.333333333333332 }, { "2017W42": 20 }, { "2017W42": 20.5 }, { "2017W43": 20 }, { "2017W43": 20 }, { "2017W43": 19.666666666666668 }, { "2017W43": 19.6 }, { "2017W43": 19.666666666666668 }, { "2017W43": 19 }, { "2017W43": 19.5 }, { "2017W44": 21 }, { "2017W44": 19.5 }, { "2017W44": 20 }, { "2017W44": 19.5 }] console.log(getValueAvg(data)); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM