根据angularjs中的键和最新时间​​戳键过滤json

时间:2018-05-28 10:48:00

标签: javascript angularjs json

我有一个JSON文件,它是从包含不同应用程序名称和时间戳以及其他详细信息的远程api中获取的。我需要使用两个过滤器来过滤这个json,即App name + latest timestamp。 json文件如下所示:

{
    "data": [
        {
          "appDetails": [
            {
              "appDescription": [
                {
                  "appName": "App 1",
                  "timeStamp": "2018-05-28T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.24",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 1",
                  "timeStamp": "2018-05-27T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.23",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 2",
                  "timeStamp": "2018-05-26T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.22",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 2",
                  "timeStamp": "2018-05-28T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.3",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 2",
                  "timeStamp": "2018-05-26T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.2",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 3",
                  "timeStamp": "2018-05-27T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.0.9",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 3",
                  "timeStamp": "2018-05-25T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.0.8",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 4",
                  "timeStamp": "2018-05-28T00:01:01.001Z",
                  "GitTag": "RELEASE-1.1.1.8",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 4",
                  "timeStamp": "2018-05-25T00:01:01.001Z",
                  "GitTag": "RELEASE-1.1.1.6",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                }
              ]
            }
          ]
        }
      ]
    }

例如,我需要一个来自上面json的过滤数组,同时拾取应用程序名称和最新时间​​戳。

filteredArray = [{
                  "appName": "App 1",
                  "timeStamp": "2018-05-28T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.24",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                  },
                  {
                  "appName": "App 2",
                  "timeStamp": "2018-05-28T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.1.3",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 3",
                  "timeStamp": "2018-05-27T00:01:01.001Z",
                  "GitTag": "RELEASE-1.0.0.9",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                },
                {
                  "appName": "App 4",
                  "timeStamp": "2018-05-28T00:01:01.001Z",
                  "GitTag": "RELEASE-1.1.1.8",
                  "blocker": 3,
                  "critical": 4,
                  "major": 30,
                  "minor": 3,
                  "UnitTestsTotal": 59,
                  "FailedTests": 0,
                  "CodeCoverage": 90
                }
]

我现在提供的angularjs代码如下:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {

  $scope.tableData = [{
              "appName": "App 1",
              "timeStamp": "2018-05-28T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.1.24",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 1",
              "timeStamp": "2018-05-27T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.1.23",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 2",
              "timeStamp": "2018-05-26T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.1.22",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 2",
              "timeStamp": "2018-05-28T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.1.3",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 2",
              "timeStamp": "2018-05-26T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.1.2",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 3",
              "timeStamp": "2018-05-27T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.0.9",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 3",
              "timeStamp": "2018-05-25T00:01:01.001Z",
              "GitTag": "RELEASE-1.0.0.8",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 4",
              "timeStamp": "2018-05-28T00:01:01.001Z",
              "GitTag": "RELEASE-1.1.1.8",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            },
            {
              "appName": "App 4",
              "timeStamp": "2018-05-25T00:01:01.001Z",
              "GitTag": "RELEASE-1.1.1.6",
              "blocker": 3,
              "critical": 4,
              "major": 30,
              "minor": 3,
              "UnitTestsTotal": 59,
              "FailedTests": 0,
              "CodeCoverage": 90
            }

  ]

  $scope.appNames = Array.from(new Set($scope.tableData.map(a => a.appName)));;

  $scope.expanding = {};
  for (index = 0; index < $scope.appNames.length; index++) {
    $scope.expanding[$scope.appNames[index]] = false;
  }

  $scope.swapExpanding = function(name) {
    $scope.expanding[name] = !$scope.expanding[name];
  }

}

请帮助我,因为我是javascript以及angular js的新手。任何有关基于appName键以及最新时间戳在我的angularjs代码中过滤数组的帮助都将非常感激。提前致谢。

2 个答案:

答案 0 :(得分:1)

使用reduce按名称分组,内置测试以比较时间戳:

const appDescription=[{"appName":"App 1","timeStamp":"2018-05-28T00:01:01.001Z","GitTag":"RELEASE-1.0.1.24","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 1","timeStamp":"2018-05-27T00:01:01.001Z","GitTag":"RELEASE-1.0.1.23","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 2","timeStamp":"2018-05-26T00:01:01.001Z","GitTag":"RELEASE-1.0.1.22","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 2","timeStamp":"2018-05-28T00:01:01.001Z","GitTag":"RELEASE-1.0.1.3","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 2","timeStamp":"2018-05-26T00:01:01.001Z","GitTag":"RELEASE-1.0.1.2","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 3","timeStamp":"2018-05-27T00:01:01.001Z","GitTag":"RELEASE-1.0.0.9","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 3","timeStamp":"2018-05-25T00:01:01.001Z","GitTag":"RELEASE-1.0.0.8","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 4","timeStamp":"2018-05-28T00:01:01.001Z","GitTag":"RELEASE-1.1.1.8","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90},{"appName":"App 4","timeStamp":"2018-05-25T00:01:01.001Z","GitTag":"RELEASE-1.1.1.6","blocker":3,"critical":4,"major":30,"minor":3,"UnitTestsTotal":59,"FailedTests":0,"CodeCoverage":90}];
const filteredAppsByName = appDescription.reduce((a, app) => {
  const { appName, timeStamp } = app;
  if (
    !a[appName]
    || (new Date(a[appName].timeStamp) < new Date(timeStamp))
  ) a[appName] = app;
  return a;
}, {});
const output = Object.values(filteredAppsByName);
console.log(output);

答案 1 :(得分:1)

  • 循环使用forEach
  • 的项目
  • 如果数组中不存在数据,请添加
  • 否则比较时间戳,如果条件满足,则用新的数据替换旧数据

&#13;
&#13;
var x = { "data": [{ "appDetails": [{ "appDescription": [{ "appName": "App 1", "timeStamp": "2018-05-28T00:01:01.001Z", "GitTag": "RELEASE-1.0.1.24", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 1", "timeStamp": "2018-05-27T00:01:01.001Z", "GitTag": "RELEASE-1.0.1.23", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 2", "timeStamp": "2018-05-26T00:01:01.001Z", "GitTag": "RELEASE-1.0.1.22", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 2", "timeStamp": "2018-05-28T00:01:01.001Z", "GitTag": "RELEASE-1.0.1.3", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 2", "timeStamp": "2018-05-26T00:01:01.001Z", "GitTag": "RELEASE-1.0.1.2", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 3", "timeStamp": "2018-05-27T00:01:01.001Z", "GitTag": "RELEASE-1.0.0.9", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 3", "timeStamp": "2018-05-25T00:01:01.001Z", "GitTag": "RELEASE-1.0.0.8", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 4", "timeStamp": "2018-05-28T00:01:01.001Z", "GitTag": "RELEASE-1.1.1.8", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 }, { "appName": "App 4", "timeStamp": "2018-05-25T00:01:01.001Z", "GitTag": "RELEASE-1.1.1.6", "blocker": 3, "critical": 4, "major": 30, "minor": 3, "UnitTestsTotal": 59, "FailedTests": 0, "CodeCoverage": 90 } ] }] }] };

var filtered_x = [];
x["data"][0]["appDetails"][0]["appDescription"].forEach(function(itm) {
  var index = filtered_x.findIndex(function(iitt) {
    return itm["appName"] == iitt["appName"];
  });
  if (index == -1) {
    filtered_x.push(itm);
  } else if(new Date(filtered_x[index]["timeStamp"]) < new Date(itm["timeStamp"])){
    filtered_x[index] = itm;
  }
});

console.log(filtered_x)
&#13;
&#13;
&#13;