首页 > 解决方案 > 如何按天获得最大值?

问题描述

这似乎是一个简单的问题,但我找不到一个好的解决方案。正如您在下面的片段中看到的,我按天和代码对数组进行分组。这是我想做的。但我有一条规则要执行:如果 A > 6 或 B > 60在一天中的任何时候(仅当代码是 GLLD008)那么 GLLD008 应该变成 GLLD0015(A 和 B 是 varsyscode)

输出应该是:

  5 - Thu Sep 06 2018 16:03:10 GMT+0200 (CEST) - GLLD008 - A ( Because A <=6 & no B => GLLD008)
  7 - Mon Sep 10 2018 13:32:28 GMT+0200 (CEST) - GLLD015 - A ( Because A > 6 => GLLD015)
  85 - Wed Sep 05 2018 15:07:00 GMT+0200 (Central European Summer Time) - GLLD015 - B

如果有人可以帮忙

谢谢

var myArray = [
  {
    "value": 5,
    "datetime": 1536242590000,
    "code": "GLLD008",
    "varsyscode": "A"
  },
  {
    "value": 7,
    "datetime": 1536579148000,
    "code": "GLLD008",
    "varsyscode": "A"
  },
  {
    "value": 6,
    "datetime": 1536579688000,
    "code": "GLLD008",
    "varsyscode": "A"
  },
  {
    "value": 5,
    "datetime": 1536833430000,
    "code": "GLLD008",
    "varsyscode": "A"
  },
  {
    "value": 85,
    "datetime": 1536152820000,
    "code": "GLLD008",
    "varsyscode": "B"
  },
  {
    "value": 69,
    "datetime": 1536154560000,
    "code": "GLLD008",
    "varsyscode": "B"
  },
  {
    "value": 73,
    "datetime": 1536571560000,
    "code": "GLLD008",
    "varsyscode": "B"
  },
  {
    "value": 69,
    "datetime": 1536575160000,
    "code": "GLLD008",
    "varsyscode": "B"
  }
];

var helper = {};
// Group by day and code
var result = myArray.reduce(function(r, o) {
        var day = new Date(o.datetime).getFullYear() + '/' + new Date(o.datetime).getMonth() + '/' + new Date(o.datetime).getDay();
        var key = day + '-' + o.code;
        
        if (!helper[key]) {
            helper[key] = Object.assign({}, o);
            r.push(helper[key]);
        }
        return r;
    }, []);


result.forEach(function(element) {
    console.log(element.value + " - " + (new Date(element.datetime).toString()) + " - " + element.code + " - " + element.varsyscode);
});

标签: javascriptarrays

解决方案


您需要执行几个步骤才能更新code.

  1. 检查是否code"GLLD008"
  2. 检查是否value为数字。
  3. 评估varsyscode
    1. 评估value
    2. 更新code

您可以在生成key.

if (o.code === 'GLLD008' && isNumeric(o.value)) {
  switch (o.varsyscode) {
    case 'A':
      if (o.value > 6) {
        o.code = 'GLLD0015'
      }
      break;
    case 'B':
      if (o.value > 60) {
        o.code = 'GLLD0015'
      }
      break;
  }
} 

执行

var myArray = [
  { "value": 5,      "datetime": 1536242590000, "code": "GLLD008", "varsyscode": "A" },
  { "value": 7,      "datetime": 1536579148000, "code": "GLLD008", "varsyscode": "A" },
  { "value": 6,      "datetime": 1536579688000, "code": "GLLD008", "varsyscode": "A" },
  { "value": 5,      "datetime": 1536833430000, "code": "GLLD008", "varsyscode": "A" },
  { "value": 85,     "datetime": 1536152820000, "code": "GLLD008", "varsyscode": "B" },
  { "value": 69,     "datetime": 1536154560000, "code": "GLLD008", "varsyscode": "B" },
  { "value": 73,     "datetime": 1536571560000, "code": "GLLD008", "varsyscode": "B" },
  { "value": 69,     "datetime": 1536575160000, "code": "GLLD008", "varsyscode": "B" },
  { "value": "APRV", "datetime": 1536152820000, "code": "GLLD008", "varsyscode": "C" },
  { "value": "APRV", "datetime": 1536575040000, "code": "GLLD008", "varsyscode": "C" },
  { "value": "APRV", "datetime": 1536590700000, "code": "GLLD008", "varsyscode": "C" }
];

var helper = {};
// Group by day and code
var result = myArray.reduce((r, o) => {
  var day = formatDate(o.datetime);
  if (o.code === 'GLLD008' && isNumeric(o.value)) {
    switch (o.varsyscode) {
      case 'A':
        if (o.value > 6) {
          o.code = 'GLLD0015'
        }
        break;
      case 'B':
        if (o.value > 60) {
          o.code = 'GLLD0015'
        }
        break;
    }
  }
  var key = day + '-' + o.code;
  if (!helper[key]) {
    helper[key] = Object.assign({}, o);
    r.push(helper[key]);
  }
  return r;
}, []);

var grouped = result.reduce((o, curr) => {
  let key = formatDate(curr.datetime);
  
  if (!isNumeric(curr.value)) {
    return o; // skip non-numeric value e.g. "APRV"
  }
  
  if (o[key] == null) {
    return Object.assign(o, { [key] : curr }); // store initial entry
  }
  
  let prev = o[key]; // get stored value for key

  // If you wanted the max value... (this is overriden below)
  if (curr.value > prev.value) {
    o[key] = curr;
  }

  // If you only want to keep "GLLD008"
  if (curr.code === 'GLLD008' && prev.code === 'GLLD0015') {
    o[key] = curr;
  }

  return o;
}, {});

result = Object.keys(grouped).map(key => grouped[key]); // map to list
result.sort((a, b) => new Date(a.datetime) - new Date(b.datetime)); // sort list

result.forEach(entry => {
  console.log([
    entry.value, formatDate(entry.datetime), entry.code, entry.varsyscode
  ].join(' - '));
});

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function formatDate(date) {
  return ((d) => d.getFullYear() + '/' + d.getMonth() + '/' + d.getDate())(new Date(date));
}
//Edited


推荐阅读