过滤AngularJS中的JSON数据

时间:2017-08-23 03:13:11

标签: angularjs json angularjs-filter

有人可以帮我解决我面临的问题,我的主要目的是根据名称和创建的字段过滤数据,我想得到名称为IN PROGRESS的计数并用此创建字段我可以轻松地在Angular JS中使用任何简单的方法,而且现在我只获得IN PROGRESS字段,但将来我也需要使用其他值进行过滤。

Plunker演示

Plunker

JS

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

app.controller("Controller", ["$scope", "$http", "$filter", "$window",
      function ($scope, $http, $filter, $window) {

        $scope.findTheValue = function () {

            $http({
                method: 'GET',
                url: 'issues.json'
            }).then(function (response) {
                $scope.selectedCount = $filter('filter')(response.data.issues, function (inputs) {
                    if (inputs.fields.status.name == 'IN PROGRESS')
                        return inputs;
                });
                console.log($scope.selectedCount.length);
                $scope.dayCount = 0;
                $scope.monthEventCount = 0;

                // Finding the The value of Day, Week and Month With Respect to Today's Date
                var todayAsBase = new Date();
                var todayAsBaseday = todayAsBase.getDay();
                var todayAsBaseWeek = getWeekNumber(todayAsBase)[0];
                var todayAsBaseMonth = todayAsBase.getMonth();
                console.log(todayAsBaseday, todayAsBaseWeek, todayAsBaseMonth);
                $scope.dayEventCount = 0;
                $scope.weekEventCount = 0;
                $scope.monthEventCount = 0;
                angular.forEach(response.data.issues, function (issue) {
                    issueDate = new Date(issue.fields.created);
                    day = issueDate.getDay();
                    week = getWeekNumber(issueDate)[0];
                    month = issueDate.getMonth();

                    if (week == todayAsBaseWeek)
                        $scope.weekEventCount = $scope.weekEventCount + 1;
                    if (month == todayAsBaseMonth)
                        $scope.monthEventCount = $scope.monthEventCount + 1;
                    if (day == todayAsBaseday && week == todayAsBaseWeek && month == todayAsBaseMonth)
                        $scope.dayEventCount = $scope.dayEventCount + 1;
                });
                console.log($scope.dayEventCount, $scope.weekEventCount, $scope.monthEventCount);
            })
        }


      }
    ]);

function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
    // Get first day of year
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
    // Return array of year and week number
    return [weekNo];
}

1 个答案:

答案 0 :(得分:1)

class MethodViewController: UIViewController {

    var items: [[String: Any]]? {
        didSet {
           print(items) // PRINTS OUT ITEMS NORMALLY
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print(items) // PRINTS OUT NIL       
    }
}

就像是时刻()。格式('MM')==时刻(已创建).format('MM')