使用javascript按月份和年份创建内部数组组

使用javascript按月份和年份创建内部数组组,javascript,arrays,Javascript,Arrays,我有一个数组,看起来像: var v = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"] 我要做的是将其动态排序到一个新的空数组nv。排序完成后,nv应如下所示 var nv = [["07/27/2015", "07/28/2015"], ["08/29/2015", "08/29/2015"], ["07/27/2016"]] 可以这样排序吗?您可以循环数组,检查每个值是否有新的月份和年份,或者

我有一个数组,看起来像:

var v = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"]
我要做的是将其动态排序到一个新的空数组
nv
。排序完成后,
nv
应如下所示

var nv = [["07/27/2015", "07/28/2015"], ["08/29/2015", "08/29/2015"], ["07/27/2016"]]

可以这样排序吗?

您可以循环数组,检查每个值是否有新的月份和年份,或者它已经包含在已排序的数组中。我认为这是一个未经测试的代码:

new_arr = new Array();
for(var i=0; i < v.length; i++){
    var this_date = new Date(v[i]);
    var month_and_year = this_date.getMonth() + this_date.getFullYear();
    if(typeof(new_arr[month_and_year]) == 'undefined'){
         new_arr[month_and_year] = new Array();
    }
    new_arr[month_and_year].push(v[i])
} 
new_arr=new Array();
对于(变量i=0;i
您可以循环数组,检查每个值是否有新的月份和年份,或者它是否已包含在排序数组中。我认为这是一个未经测试的代码:

new_arr = new Array();
for(var i=0; i < v.length; i++){
    var this_date = new Date(v[i]);
    var month_and_year = this_date.getMonth() + this_date.getFullYear();
    if(typeof(new_arr[month_and_year]) == 'undefined'){
         new_arr[month_and_year] = new Array();
    }
    new_arr[month_and_year].push(v[i])
} 
new_arr=new Array();
对于(变量i=0;i


因此,我制作了一个函数,将日期放入一个属性为月份和年份的对象中。日期被放入其月份和年份的属性中。然后,函数创建一个数组,并为函数的每个属性创建一个内部数组。在每个内部数组中,它放置该属性的所有日期。我认为这种方法比嵌套for循环更有效

// function takes an array of dates in the following format MM/DD/YYYY
// outputs an array with inner arrays of dates. Each inner array contains dates of the same month and year
var groupDates = function(dateArray) {
    // create object to organize dates by month and year
    var dateHash = {};
    // the array that is outputted
    var groupedDates = [];

    //for every date in dateArray
    dateArray.forEach(function(currentDate) {
        // check if any other dates with the same month and year exist in the dateHash object
        if (dateHash[currentDate.substr(0, 2) + currentDate.substr(6)]) {
            // if other dates exist, push the date to the array in the dateHash property for the dates current month and year
            dateHash[currentDate.substr(0, 2) + currentDate.substr(6)].push(currentDate);
        } else {
            // otherwise create a property for the dates month and year and store the current date in an array in the propery
            dateHash[currentDate.substr(0, 2) + currentDate.substr(6)] = [currentDate];
        }
    });

    // for every propery in the datehash, push the array of dates into the grouped dates array
    for (var dateGroup in dateHash) {
        groupedDates.push(dateHash[dateGroup]);
    }
    return groupedDates;
};

var dateArray = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"];
console.log(groupDates(dateArray));

所以我做了一个函数,将日期放入一个对象中,该对象的属性是月和年。日期被放入其月份和年份的属性中。然后,函数创建一个数组,并为函数的每个属性创建一个内部数组。在每个内部数组中,它放置该属性的所有日期。我认为这种方法比嵌套for循环更有效

// function takes an array of dates in the following format MM/DD/YYYY
// outputs an array with inner arrays of dates. Each inner array contains dates of the same month and year
var groupDates = function(dateArray) {
    // create object to organize dates by month and year
    var dateHash = {};
    // the array that is outputted
    var groupedDates = [];

    //for every date in dateArray
    dateArray.forEach(function(currentDate) {
        // check if any other dates with the same month and year exist in the dateHash object
        if (dateHash[currentDate.substr(0, 2) + currentDate.substr(6)]) {
            // if other dates exist, push the date to the array in the dateHash property for the dates current month and year
            dateHash[currentDate.substr(0, 2) + currentDate.substr(6)].push(currentDate);
        } else {
            // otherwise create a property for the dates month and year and store the current date in an array in the propery
            dateHash[currentDate.substr(0, 2) + currentDate.substr(6)] = [currentDate];
        }
    });

    // for every propery in the datehash, push the array of dates into the grouped dates array
    for (var dateGroup in dateHash) {
        groupedDates.push(dateHash[dateGroup]);
    }
    return groupedDates;
};

var dateArray = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"];
console.log(groupDates(dateArray));

标准是什么?按月分组,我想…我想对数组进行排序,如果日期在同一个月和同一年下,则创建一个新的内部数组/按月和年分组。在问题和标题中澄清这一点。标准是什么?按月分组,我想…我想对数组进行排序,如果日期在同一个月和同一年/一年一个月和一年一个组下,则创建一个新的内部数组。请在问题和标题中澄清这一点。
Uncaught TypeError:无法读取未定义的属性“push”
try typeof(new_arr[month_和_year])==“undefined”。我编辑了答案
uncaughttypeerror:无法读取未定义的属性“push”
try-typeof(new\u arr[月和年])==“未定义”。我把答案编辑得非常干净。但不是一般的。当日期格式不同时会发生什么?或者它们是不同日期格式的组合?看起来很完美。再次感谢@Andreas@Anurag或者潜在的日历变化,或者人类灭绝…^^^那么它必须相应地进行调整:)我们只能使用提供的信息,它们只包含一种日期格式。如果有其他日期格式,我建议使用momentjs等日期库将日期字符串转换为实际日期。但这只会影响对象键的创建。真正干净的答案。但不是一般的。当日期格式不同时会发生什么?或者它们是不同日期格式的组合?看起来很完美。再次感谢@Andreas@Anurag或者潜在的日历变化,或者人类灭绝…^^^那么它必须相应地进行调整:)我们只能使用提供的信息,它们只包含一种日期格式。如果有其他日期格式,我建议使用momentjs等日期库将日期字符串转换为实际日期。但这只会影响对象关键点的创建。