NodeJS 如何为特定键循环,以确定该键在对象数组中出现的次数

0mkxixxg  于 2023-01-08  发布在  Node.js
关注(0)|答案(2)|浏览(138)

我目前正在一个物联网项目,需要计算平均15分钟的窗口开始:-

| Blocks         | Average               |
| 00:00 - 00:15  | average of this block |
| 00:15 - 00:30  | average of this block |
| 00:30 - 00:45  | average of this block |
| 00:45 - 01:00  | average of this block |

等等。我已经实现了一个解决方案。流程是这样的。
第一个月
我的lambda代码是为了计算有效载荷中不同站点(设备)的数量,并计算每个站点(设备)的出现次数,因为我必须以时间块的方式计算多个站点或设备的平均值这里是我的测试事件,我已经配置了现在测试功能:-

[
  {
    "site_name": "adodar",
    "active-power": 23.4
  },
  {
    "site_name": "adodar",
    "active-power": 10.4
  },
  {
    "site_name": "adodar",
    "active-power": 2.4
  },
  {
    "site_name": "adodar",
    "active-power": 3.4
  },
  {
    "site_name": "adodar",
    "active-power": 11.4
  },
  {
    "site_name": "adodar",
    "active-power": 0.4
  },
  {
    "site_name": "jamanvada",
    "active-power": 6.4
  },
  {
    "site_name": "jamanvada",
    "active-power": 0.5
  },
  {
    "site_name": "jamanvada",
    "active-power": 4.55
  },
  {
    "site_name": "abcd",
    "active-power": 9.08
  }
]

下面是我的lambda代码来计算每个站点的出现次数:-

export const handler = async(event) => {
    // TODO implement
    let occurences = {};
    let arr = event;
    arr.map( data => {
      occurences[data.site_name]=(occurences[data.site_name] || 0)+1;
    });
    console.log(occurences);
};

在这里,我得到了正确的输出,如预期的计数记录的数量为每个设备或网站,这里是我的输出:-

{ adodar: 6, jamanvada: 3, abcd: 1 }

现在,我想对每个站点或设备循环执行与出现次数相同的次数,并计算其平均值。例如,设备adaodar出现了6次,因此公式为

add all 6 occurences / number of occurences for each site.

例如,adodar有6个示例,我想在这6个对象上循环,并计算"有功功率"字段的平均值。
但我做不到。
任何帮助都将不胜感激。

omjgkv6w

omjgkv6w1#

您应该能够通过存储每个站点的active-power总和和计数来计算平均值:

export const handler = async (event) => {
  let occurences = {};
  event.forEach(data => {
    if (occurences[data.site_name] === undefined) {
      occurences[data.site_name] = {
        activePowerSum: 0.0,
        count: 0
      };
    }
    occurences[data.site_name].activePowerSum += data['active-power'];
    occurences[data.site_name].count++;
  });
  let averages = {};
  for (const key in occurences) {
    averages[key] = occurences[key].activePowerSum / occurences[key].count;
  }
  console.log(occurences, averages);
};
x4shl7ld

x4shl7ld2#

我用occurences迭代数组中的每一个值,然后将值的和除以它的长度:

> siteValues = {}
> Object.keys(occurences).forEach(key => { siteValues[key] = array.filter(x=>x.site_name==key).map(value => value['active-power'])});
undefined
> siteValues
{
  adodar: [ 23.4, 10.4, 2.4, 3.4, 11.4, 0.4 ],
  jamanvada: [ 6.4, 0.5, 4.55 ],
  abcd: [ 9.08 ]
}

为了得到每个站点名称的总和,我使用了reduce,并再次使用array.filter除以数组长度(可能有一些额外的计算操作):

> Object.keys(occurences).forEach(key => { siteValues[key] = array.filter(x=>x.site_name==key).map(value => value['active-power']).reduce(function(a, b) { return a + b; }, 0) / array.filter(x=>x.site_name==key).length});
undefined
> siteValues
{
  adodar: 8.566666666666665,
  jamanvada: 3.8166666666666664,
  abcd: 9.08
}

其等效于:{ adodar: 51.39999999999999 / 6, jamanvada: 11.45 / 3, abcd: 9.08 / 1 }

相关问题