使用日期时间对 JSON 数据进行分组

group json data using date time

本文关键字:数据 JSON 日期 时间      更新时间:2023-09-26

我需要按日期,月份,周和日对我的json数据进行分组。我使用 REST API,无法获取分组的 json 数据。我需要按月、周和天对数据进行分组:所以它是一个级联 JSON 对象

年份:{ 月份:{

day1:{data1, data2...}, 第2天:{..}}, 月2:{..}},

任何其他想法。

这是我获取数据的第一个函数:

getListItems(_spPageContextInfo.webAbsoluteUrl, "Activites", "", FillFormat_JSONDate, "");

function getListItems(url, listname, query, complete, failure) {
    // Executing our items via an ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results
        },
        error: function (data) {
            //failure(data);
            alert("failure");
        }
    });
}

function FillFormat_JSONDate(dataitems) {
    var jsonStr = JSON.stringify(dataitems.d.results);
    var jsonPars = JSON.parse(jsonStr);
    //map data from SPList to Array
    var jsonYearformat = {};
    var SPListItemP = $.map(jsonPars, function (item) {
        var date = new Date(item.Created);
      //  push data into jsonYearformat
        return false;
    });
}  

我使用此代码来获取分组:

 i = 0,
    groups = {};
    var year;
    var day;
    var month;
    var SPListItemP = $.map(jsonPars, function (item) {
        elt = new Date(item.IPS_START);
            year = elt.getFullYear();
            month = elt.getMonth() + 1;
            day = elt.getDate();
            groups[year] || (groups[year] = {}); // exists OR create {}
            groups[year][month] || (groups[year][month] = []);
            groups[year][month][day] || (groups[year][month][day] = []);  // exists OR create []
            groups[year][month][day].push(item);
        return false;
    });