获取具有相同值的键集

时间:2017-07-23 19:40:05

标签: javascript arrays json

我有javascript对象数组:

objArray = [
  {"date":"07/19/2017 12:00:00 AM","count":"1000","code":"K100"},
  {"date":"07/21/2017 12:00:00 AM","count":"899","code":"C835"},
  {"date":"07/23/2017 12:00:00 AM","count":"700","code":"C837"},
  {"date":"07/23/2017 12:00:00 AM","count":"800","code":"K100"},
  {"date":"07/23/2017 12:00:00 AM","count":"50","code":"C837"}
];

我希望关键日期的所有值都具有与下面相同的值

  • 删除重复的日期值
  • 将代码值组合为数组
  • 总结重复日期值的计数值

这是预期的输出:

newObjArray = [
  {"date":"07/19/2017 12:00:00 AM","count":"1000","code":"K100"},
  {"date":"07/21/2017 12:00:00 AM","count":"899","code":"C835"},
  {"date":"07/23/2017 12:00:00 AM","count":"1550","code":["C837","K100","C837"]}
]

我尝试过的方式可以获得预期的输出。任何可以帮助我处理我以获得所需输出的人。

Example

1 个答案:

答案 0 :(得分:5)

    for (LogEvent event : MyAppenderForTesting.logEvents) {
        String message = event.getMessage().toString();
        if (message.contains(search)) {
            // do somethind
        }
    }