Javascript 按年份/日期排序json(json/nodeJS)

Javascript 按年份/日期排序json(json/nodeJS),javascript,json,node.js,sorting,date,Javascript,Json,Node.js,Sorting,Date,嘿,我见过这样的json news { title: 'hello world', body: 'bla bla bla', publish_date: '2014-04-12' }, { title: 'hello world', body: 'bla bla bla', publish_date: '2014-04-30' } { ti

嘿,我见过这样的json

news
    {
        title: 'hello world',
        body: 'bla bla bla',
        publish_date: '2014-04-12'
    },
    {
        title: 'hello world',
        body: 'bla bla bla',
        publish_date: '2014-04-30'
    }   
    {
        title: 'hello world 2',
        body: 'bla bla bla',
        publish_date: '2015-02-30'
    },
    {
        title: 'hello world 2',
        body: 'bla bla bla',
        publish_date: '2015-02-17'
    },
    {
        title: 'hello world 2',
        body: 'bla bla bla',
        publish_date: '2015-05-30'
    }
我想按“年”和“月”的“发布日期”进行排序,如下所示:

{
    2014: [{
        04: [{
            {
                title: 'hello world',
                body: 'bla bla bla',
                publish_date: '2014-04-12'
            },
            {
                title: 'hello world',
                body: 'bla bla bla',
                publish_date: '2014-04-30'
            }   
        }]
    ]}
    2015: [{
        02: [{
            {
                title: 'hello world 2',
                body: 'bla bla bla',
                publish_date: '2015-02-30'
            },
            {
                title: 'hello world 2',
                body: 'bla bla bla',
                publish_date: '2015-02-17'
            },
        }],
        05: [{
            {
                title: 'hello world 2',
                body: 'bla bla bla',
                publish_date: '2015-05-30'
            }
        ]}
    }]
}
所以我找到了这个 按日期对json进行排序,之后我得到了这个 运行我的主json,但如何“分割”日期以便完成?把它分成几年,然后是几个月?我如何“推”到我的新分类json中?我怎样才能确保每年不错过任何一个月? 一个方向或无论如何,使它完成将是可怕的:)


非常感谢你

通过原始json循环:

var newJson = [];
for (var i in originalJson) {
    var year = originalJson[i].publish_date.substring(0,4);
    var month = originalJson[i].publish_date.substring(5,2);
    if (typeof newJson[year] == "undefined") newJson[year] = [];
    if (typeof newJson[year][month] == "undefined") newJson[year][month] = [];
    newJson[year][month].push(originalJson[i]);
}
我没有试过这个。。但也许是有效的:在这种情况下,你想要的。要分割日期,您可以解析它并从中获取年份和蛾。在映射回调中,您可以根据需要将值添加到正确的对象中

i、 e


最好将这些日期转换为实际的日期对象,这样就更容易、更清晰地访问年份和月份,如下所示:

为了简洁起见,我使用了native forEach,但Lodash也可以这样做

var json = [
{
    title: 'hello world',
    body: 'bla bla bla',
    publish_date: '2014-04-12'
},
{
    title: 'hello world',
    body: 'bla bla bla',
    publish_date: '2014-04-30'
},   
{
    title: 'hello world 2',
    body: 'bla bla bla',
    publish_date: '2015-02-30'
},
{
    title: 'hello world 2',
    body: 'bla bla bla',
    publish_date: '2015-02-17'
},
{
    title: 'hello world 2',
    body: 'bla bla bla',
    publish_date: '2015-05-30'
}
];

var newJson = {};

json.forEach(function (item) {
    var date = new Date(item.publish_date),
        year = date.getUTCFullYear(),
        month = date.getUTCMonth() + 1;

    newJson[year] = newJson[year] || {};
    newJson[year][month] = newJson[year][month] || [];
    newJson[year][month].push(item);
});

console.log(newJson);
然后,在目标对象中直接断言它们的存在,或者创建并推送,或者简单地推送,如fiddle所示

此外,如果您想对月份组数组进行实际排序,您可以这样比较日期:

var a = new Date('2015-02-17');
var b = new Date('2015-05-30');
console.log(a < b);
var a=新日期('2015-02-17');
var b=新日期('2015-05-30');
控制台日志(a
你能再解释一下我该如何使用reduce吗?我必须用
分割日期。分割(“-”
但是我看不出如何减少帮助我,谢谢!嗯,我试过这个,更像是:pastebin.com/ezezNwME,我得到的是很多:,,,[]不确定,为什么。。我用trid替换mynew=[];到mynew={};然后我得到:{'2014':[]}{'2014':[],'2015':[]}谢谢你的回答完美的非常感谢你!我有一个问题是,| |代表什么
newJson[year]=newJson[year]**| |**{}我知道它是“或”的运算符,但它在这里做什么?好像是?真:假?请解释:)谢谢@growth-如果newJson[year]不存在,则为其分配一个空对象(如果确实存在,则保持不变)。否则,接下来的两行将产生未定义的错误。如果尚未设置,
newJson[year][month]
的初始空数组将在下一行中创建。
var a = new Date('2015-02-17');
var b = new Date('2015-05-30');
console.log(a < b);
function custom_sort(a, b) {
    return new Date(a.publish_date).getTime() - new Date(b.publish_date).getTime();
}
var json = [
{
    title: 'hello world',
    body: 'bla bla bla',
    publish_date: '2014-04-12'
},
{
    title: 'hello world',
    body: 'bla bla bla',
    publish_date: '2014-04-30'
},   
{
    title: 'hello world 2',
    body: 'bla bla bla',
    publish_date: '2015-02-30'
},
{
    title: 'hello world 2',
    body: 'bla bla bla',
    publish_date: '2015-02-17'
},
{
    title: 'hello world 2',
    body: 'bla bla bla',
    publish_date: '2015-05-30'
}
];

json .sort(custom_sort);