比较同一数组中的对象JavaScript

怀斯(Kaise White)

我正在尝试比较同一数组中的对象。我有一个CSV文件,该文件在定界列表中包含一些数据。提取文件后,我需要根据以下两个条件对其进行过滤并将输出打印到屏幕或控制台:

如果对于相同的SatelliteID,在五分钟的间隔内有三个读数低于红色下限。

如果对于同一颗卫星,有五个恒温器读数在五分钟的间隔内超过红色上限。

我相信我可以先使用array.filter然后再使用array.map来获得所需的输出,但不确定从哪里遍历数组并比较对象,该从哪里走。

这是期望的结果:

[
    {
        "satelliteId": 1000,
        "severity": "RED HIGH",
        "component": "TSTAT",
        "timestamp": "2018-01-01T23:01:38.001Z"
    },
    {
        "satelliteId": 1000,
        "severity": "RED LOW",
        "component": "BATT",
        "timestamp": "2018-01-01T23:01:09.521Z"
    }
]

CSV文件如下所示:

timestamp|satelliteid|redhigh|yellowhigh|yellowlow|redlow|rawvalue|component
20180101 23:01:05.001|1001|101|98|25|20|99.9|TSTAT
20180101 23:01:09.521|1000|17|15|9|8|7.8|BATT
20180101 23:01:26.011|1001|101|98|25|20|99.8|TSTAT
20180101 23:01:38.001|1000|101|98|25|20|102.9|TSTAT
20180101 23:01:49.021|1000|101|98|25|20|87.9|TSTAT
20180101 23:02:09.014|1001|101|98|25|20|89.3|TSTAT
20180101 23:02:10.021|1001|101|98|25|20|89.4|TSTAT
20180101 23:02:11.302|1000|17|15|9|8|7.7|BATT
20180101 23:03:03.008|1000|101|98|25|20|102.7|TSTAT
20180101 23:03:05.009|1000|101|98|25|20|101.2|TSTAT
20180101 23:04:06.017|1001|101|98|25|20|89.9|TSTAT
20180101 23:04:11.531|1000|17|15|9|8|7.9|BATT
20180101 23:05:05.021|1001|101|98|25|20|89.9|TSTAT
20180101 23:05:07.421|1001|17|15|9|8|7.9|BATT

这是我编写的用于提取文件并解析它的代码。

const readFile = require("fs").readFile;
readFile("./data.csv", "utf-8", (err, data) => {
  //Store final results
  var result = [];

  //Split Each line in the CVS
  const lines = data.split("\n");

  //Assumes the first line in the csv is the header
  var headers = lines[0].split("|");

  //Create value pair
  for (var i = 1; i < lines.length; i++) {
    var obj = {};
    var currentline = lines[i].split("|");

    for (var j = 0; j < headers.length; j++) {
      obj[headers[j]] = currentline[j];
    }

    //Add objects to result array
    result.push(obj);
  }
  console.log(result);
});

到目前为止,这是解析CSV文件后记录到控制台的内容。

[
  {
    timestamp: '20180101 23:01:05.001',
    satelliteid: '1001',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '99.9',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:01:09.521',
    satelliteid: '1000',
    redhigh: '17',
    yellowhigh: '15',
    yellowlow: '9',
    redlow: '8',
    rawvalue: '7.8',
    component: 'BATT'
  },
  {
    timestamp: '20180101 23:01:26.011',
    satelliteid: '1001',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '99.8',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:01:38.001',
    satelliteid: '1000',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '102.9',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:01:49.021',
    satelliteid: '1000',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '87.9',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:02:09.014',
    satelliteid: '1001',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '89.3',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:02:10.021',
    satelliteid: '1001',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '89.4',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:02:11.302',
    satelliteid: '1000',
    redhigh: '17',
    yellowhigh: '15',
    yellowlow: '9',
    redlow: '8',
    rawvalue: '7.7',
    component: 'BATT'
  },
  {
    timestamp: '20180101 23:03:03.008',
    satelliteid: '1000',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '102.7',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:03:05.009',
    satelliteid: '1000',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '101.2',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:04:06.017',
    satelliteid: '1001',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '89.9',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:04:11.531',
    satelliteid: '1000',
    redhigh: '17',
    yellowhigh: '15',
    yellowlow: '9',
    redlow: '8',
    rawvalue: '7.9',
    component: 'BATT'
  },
  {
    timestamp: '20180101 23:05:05.021',
    satelliteid: '1001',
    redhigh: '101',
    yellowhigh: '98',
    yellowlow: '25',
    redlow: '20',
    rawvalue: '89.9',
    component: 'TSTAT'
  },
  {
    timestamp: '20180101 23:05:07.421',
    satelliteid: '1001',
    redhigh: '17',
    yellowhigh: '15',
    yellowlow: '9',
    redlow: '8',
    rawvalue: '7.9',
    component: 'BATT'
  }
]
iota

您可以reduce一起使用filter

const arr = [ { timestamp: '20180101 23:01:05.001', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '99.9', component: 'TSTAT' }, { timestamp: '20180101 23:01:09.521', satelliteid: '1000', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.8', component: 'BATT' }, { timestamp: '20180101 23:01:26.011', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '99.8', component: 'TSTAT' }, { timestamp: '20180101 23:01:38.001', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '102.9', component: 'TSTAT' }, { timestamp: '20180101 23:01:49.021', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '87.9', component: 'TSTAT' }, { timestamp: '20180101 23:02:09.014', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.3', component: 'TSTAT' }, { timestamp: '20180101 23:02:10.021', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.4', component: 'TSTAT' }, { timestamp: '20180101 23:02:11.302', satelliteid: '1000', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.7', component: 'BATT' }, { timestamp: '20180101 23:03:03.008', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '102.7', component: 'TSTAT' }, { timestamp: '20180101 23:03:05.009', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '101.2', component: 'TSTAT' }, { timestamp: '20180101 23:04:06.017', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.9', component: 'TSTAT' }, { timestamp: '20180101 23:04:11.531', satelliteid: '1000', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.9', component: 'BATT' }, { timestamp: '20180101 23:05:05.021', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.9', component: 'TSTAT' }, { timestamp: '20180101 23:05:07.421', satelliteid: '1001', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.9', component: 'BATT' } ];
const res = arr.reduce((acc, {timestamp,satelliteid:satelliteId,redlow,redhigh,component})=>{
  const getDate = str => new Date(str.slice(0,4)+'-'+str.slice(4,6)+'-'+str.slice(6));
  const date = getDate(timestamp);
  let severity;
  if(arr.filter(obj=>+obj.rawvalue < +redlow && getDate(obj.timestamp) - date <= 5 * 60 * 1000).length >= 3){
    severity = 'RED LOW';
  } else if(arr.filter(obj=>+obj.rawvalue > +redhigh && getDate(obj.timestamp) - date <= 5 * 60 * 1000).length >= 3){
    severity = 'RED HIGH';
  }
  if(severity){
    acc.push({satelliteId,severity,component,timestamp});
  }
  return acc;
}, []);
console.log(res);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将对象值与同一数组中的其他对象进行比较

来自分类Dev

如何通过比较同一数组中的两个键从数组中删除重复的对象?

来自分类Dev

根据Javascript中的公共值合并同一数组中的对象

来自分类Dev

更改同一数组中的另一个对象时,正在更改的Javascript对象

来自分类Dev

指针比较在C中如何工作?比较不指向同一数组的指针是否可以?

来自分类Dev

将数组值与同一数组中的其他值进行比较

来自分类Dev

比较同一数组中的元素彼此仅一次的时间复杂性?

来自分类Dev

将具有相同值的数组中的对象排序到同一数组中的一个对象

来自分类Dev

如何在共享相同未知密钥的同一数组中合并多个对象?

来自分类Dev

迭代比较同一数据框中的列值

来自分类Dev

比较同一数据框列中的值

来自分类Dev

具有相同ID的对象被推入同一数组

来自分类Dev

Javascript在对象数组中查找所有出现的名称并创建新的唯一数组

来自分类Dev

将对象数组转换为单个字符串并将其推送到同一数组中

来自分类Dev

在React中的同一数组上过滤和映射

来自分类Dev

PHP通过同一数组中的值合并

来自分类Dev

在Ruby的同一数组中串联现有值

来自分类Dev

比较数组JavaScript中的对象

来自分类Dev

通过两个具有不同键的键合并同一数组中的对象

来自分类Dev

将同一数组内的多个对象合并为一个对象

来自分类Dev

复制要添加到同一数组的最后一个数组对象

来自分类Dev

如果同一数组.net中的另一个对象中已经存在属性,如何不显示对象

来自分类Dev

遍历同一数组的多个范围

来自分类Dev

同一数组不同结果

来自分类Dev

通过python中同一数组中的每个元素添加数组中的每个元素

来自分类Dev

在数组元素中引用同一数组的另一个元素

来自分类Dev

PHP数组,在同一数组中引用一个较早的值

来自分类Dev

将数组合并到同一数组中的不同集合中

来自分类Dev

C ++两个对象,它们持有指向同一数组不同部分的指针

Related 相关文章

  1. 1

    将对象值与同一数组中的其他对象进行比较

  2. 2

    如何通过比较同一数组中的两个键从数组中删除重复的对象?

  3. 3

    根据Javascript中的公共值合并同一数组中的对象

  4. 4

    更改同一数组中的另一个对象时,正在更改的Javascript对象

  5. 5

    指针比较在C中如何工作?比较不指向同一数组的指针是否可以?

  6. 6

    将数组值与同一数组中的其他值进行比较

  7. 7

    比较同一数组中的元素彼此仅一次的时间复杂性?

  8. 8

    将具有相同值的数组中的对象排序到同一数组中的一个对象

  9. 9

    如何在共享相同未知密钥的同一数组中合并多个对象?

  10. 10

    迭代比较同一数据框中的列值

  11. 11

    比较同一数据框列中的值

  12. 12

    具有相同ID的对象被推入同一数组

  13. 13

    Javascript在对象数组中查找所有出现的名称并创建新的唯一数组

  14. 14

    将对象数组转换为单个字符串并将其推送到同一数组中

  15. 15

    在React中的同一数组上过滤和映射

  16. 16

    PHP通过同一数组中的值合并

  17. 17

    在Ruby的同一数组中串联现有值

  18. 18

    比较数组JavaScript中的对象

  19. 19

    通过两个具有不同键的键合并同一数组中的对象

  20. 20

    将同一数组内的多个对象合并为一个对象

  21. 21

    复制要添加到同一数组的最后一个数组对象

  22. 22

    如果同一数组.net中的另一个对象中已经存在属性,如何不显示对象

  23. 23

    遍历同一数组的多个范围

  24. 24

    同一数组不同结果

  25. 25

    通过python中同一数组中的每个元素添加数组中的每个元素

  26. 26

    在数组元素中引用同一数组的另一个元素

  27. 27

    PHP数组,在同一数组中引用一个较早的值

  28. 28

    将数组合并到同一数组中的不同集合中

  29. 29

    C ++两个对象,它们持有指向同一数组不同部分的指针

热门标签

归档