【问题标题】:Comparing objects within the same array JavaScript比较同一数组 JavaScript 中的对象
【发布时间】:2020-08-09 18:32:49
【问题描述】:

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

如果对于同一个卫星 ID,在五分钟间隔内有三个读数低于红色下限。

如果同一颗卫星的恒温器读数在五分钟内超过红色上限。

我相信我可以使用 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'
  }
]

【问题讨论】:

  • 这 5 分钟的间隔是多少?我认为 redhigh 或 redlow 是使用 rawvalue 决定的。对吗?
  • 是的,你使用 rawvalue 来确定它是落入高点还是低点,所以如果我要使用过滤器,我可能会使用 result.filter(item => item.rawvalue > item.redlow)或类似的东西。 5 分钟间隔涉及比较时间戳,以查看它们是否在同一 ID 的 5 分钟内

标签: javascript arrays csv


【解决方案1】:

您可以reducefilter 一起使用。

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多