【问题标题】:JSON Group by Name and then Display Latest RecordJSON 按名称分组,然后显示最新记录
【发布时间】:2019-10-26 09:06:46
【问题描述】:

只使用 JavaScript,我需要

  1. 按代码分组。
  2. 获取最新修改日期。
  3. 将总分组代码显示为 Count。

开始 JSON 结果

[
{"ID":1,"code":"AAA","modifieddate":"2019-06-01","user":"John"},
{"ID":2,"code":"AAA","modifieddate":"2019-06-02","user":"Jane"},
{"ID":3,"code":"AAA","modifieddate":"2019-06-03","user":"Sue"},
{"ID":4,"code":"BBB","modifieddate":"2019-06-10","user":"Rick"},
{"ID":5,"code":"CCC","modifieddate":"2019-06-11","user":"Joe"}
]

所需的 JSON 结果集

[
{"ID":3,"code":"AAA","modifieddate":"2019-06-03","user":"Sue","Count":"3"},
{"ID":4,"code":"BBB","modifieddate":"2019-06-10","user":"Rick","Count":"1"},
{"ID":5,"code":"CCC","modifieddate":"2019-06-11","user":"Joe","Count":"1"}
]
  1. 尝试使用 reduce 方法。
  2. 我无权修改服务器端 API 代码。
  3. 我正在使用 Aurelia JS。

【问题讨论】:

    标签: javascript json filtering


    【解决方案1】:

    您可以使用Array.reduce 对每个项目的code 属性的结果集进行分组,根据需要递增Count,然后从累积对象中获取值。在此过程中,我们会执行日期比较以确定要在结果中包含哪些最新条目。

    const data = [ {"ID":1,"code":"AAA","modifieddate":"2019-06-01","user":"John"}, {"ID":2,"code":"AAA","modifieddate":"2019-06-02","user":"Jane"}, {"ID":3,"code":"AAA","modifieddate":"2019-06-03","user":"Sue"}, {"ID":4,"code":"BBB","modifieddate":"2019-06-10","user":"Rick"}, {"ID":5,"code":"CCC","modifieddate":"2019-06-11","user":"Joe"} ];
    
    const result = Object.values(data.reduce((a, e) => {
      if (!a[e.code]) {
        a[e.code] = {...e, Count: 0};
      }
      
      if (Date.parse(e.modifieddate) > Date.parse(a[e.code].modifieddate)) {
        a[e.code] = {...e, Count: a[e.code].Count};
      }
      
      a[e.code].Count++;
      return a;
    }, {}));
    
    console.log(result);

    顺便说一句,这只是我们正在使用的普通 JS 数组,而不是 JSON。

    【讨论】:

    • 我没有看到任何关于 modifieddate 的内容。 OP 也想要最近的记录。
    • 对不起,我没有关注。我的输出与 OP 完全匹配,所以你能澄清差异在哪里吗?
    • 尝试改变前三个记录的顺序。您可以先订购它,然后它总会得到正确的结果。
    • 好的,我不太清楚 OP 的意图是什么,因为这个例子很简单。已更新以符合该要求。
    • 做得很好。谢谢戈伦先生。非常感谢先生!
    【解决方案2】:

    这应该让你:

    let array = [
      {"ID":1,"code":"AAA","modifieddate":"2019-06-01","user":"John"},
      {"ID":2,"code":"AAA","modifieddate":"2019-06-02","user":"Jane"},
      {"ID":3,"code":"AAA","modifieddate":"2019-06-03","user":"Sue"},
      {"ID":4,"code":"BBB","modifieddate":"2019-06-10","user":"Rick"},
      {"ID":5,"code":"CCC","modifieddate":"2019-06-11","user":"Joe"}
    ]
    let result = array.reduce(function(total, currentValue, currentIndex, arr) {
      let index = total.findIndex(function(entry) { return entry.code == currentValue.code; })
      if (index >= 0) { // entry already exists
        // check modified
        if (total[index].modifieddate > currentValue.modifieddate) { // already have most recent of the two
          total[index].Count += 1;
        } else { // need to replace with more recent
          currentValue.Count = total[index].Count + 1;
          total[index] = currentValue;
        }
      } else { // first record for this code
        currentValue.Count = 1;
        total.push(currentValue);
      }
      return total;
    }, []);
    console.log(result);
    

    这是一个有效的js-fiddle

    注意:注释在代码块中进行

    【讨论】:

    • 完美运行。谢谢瓦森先生。非常感谢先生!
    • 没问题@Brian
    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多