Closed. This question needs details or clarity。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                
                    3年前关闭。
            
        

    

我从数据库查询(nodejs,使用带有// Knex的Postgres的nodejs)中获得了一个对象数组,看起来像这样:(缩小版本)

[
   {
    tvshow: 'house',
    airdate: 2017-02-01T00:00:00.000Z
   },
   {
    tvshow: 'big bang theory',
    airdate: 2017-02-01T00:00:00.000Z
   },
   {
    tvshow: 'simpsons',
    airdate: 2017-02-02T00:00:00.000Z
   },
   {
    tvshow: 'suits',
    airdate: 2017-02-02T00:00:00.000Z
   },
   {
    tvshow: 'sun',
    airdate: 2017-02-03T00:00:00.000Z
   },
   {
    tvshow: 'blacklist',
    airdate: 2017-02-03T00:00:00.000Z
  },
  {
    tvshow: 'something',
    airdate: 2017-02-03T00:00:00.000Z
  },
  {
    tvshow: 'homeland',
    airdate: 2017-02-03T00:00:00.000Z
  },
  {
    tvshow: 'american dad',
    airdate: 2017-02-04T00:00:00.000Z
  },
  {
    tvshow: 'games',
    airdate: 2017-02-05T00:00:00.000Z
  }
]


返回此查询的查询将airdate字段限制为5天。这意味着无论我做什么,它总是有5天的时间。

我的目标是按天过滤电视节目。

预期输出示例

{
    day1: [ 'house', 'big bang theory' ],
    day2: [ 'simpsons', 'suits' ],
    day3: [ 'sun', 'blacklist', 'something', 'homeland' ],
    day4: [ 'american dad' ],
    day5: [ 'games' ]
}


我有一个可行的解决方案,但我认为可以改进。这就是我正在做的atm。我正在使用momentjs比较日期。 “结果”是查询结果的名称(对象数组)。

const obj = {};
let array = [];
let i = 0;
let currentDate = null;

result.forEach((element) => {
  if (currentDate !== moment(element.airdate).format('DD-MM-YYYY')) {
    if (currentDate !== null) {      // skip first iteration where currentDate is null
      i++;
      obj[`day${i}`] = array;     // new day => store previous day tvshow's array in the object
    }
    array = [];     // reset temporary array
  }
  array.push(element.tvshow);    // push tvshow to temporary array
  currentDate = moment(element.airdate).format('DD-MM-YYYY');   // update current date
});
obj[`day${i+1}`] = array;    // add last/5th day array to the object

最佳答案

我会尽量避免修改forEach回调内部定义的变量。

这是带有reduce的版本,它将所有状态存储在reduce累加器中。最后,您可以提取包含结果对象的零件。假定日期是按升序排列的,您可以在查询中处理:

result = result.reduce( (acc, {tvshow, airdate}) => {
    acc.i += acc.airdate !== airdate;
    acc.airdate = airdate;
    acc.obj[`day${acc.i}`] = (acc.obj[`day${acc.i}`] || []).concat(tvshow);
    return acc;
}, {i:0, obj: {}} ).obj;




let result = [
   {
    tvshow: 'house',
    airdate: '2017-02-01T00:00:00.000Z'
   },
   {
    tvshow: 'big bang theory',
    airdate: '2017-02-01T00:00:00.000Z'
   },
   {
    tvshow: 'simpsons',
    airdate: '2017-02-02T00:00:00.000Z'
   },
   {
    tvshow: 'suits',
    airdate: '2017-02-02T00:00:00.000Z'
   },
   {
    tvshow: 'sun',
    airdate: '2017-02-03T00:00:00.000Z'
   },
   {
    tvshow: 'blacklist',
    airdate: '2017-02-03T00:00:00.000Z'
  },
  {
    tvshow: 'something',
    airdate: '2017-02-03T00:00:00.000Z'
  },
  {
    tvshow: 'homeland',
    airdate: '2017-02-03T00:00:00.000Z'
  },
  {
    tvshow: 'american dad',
    airdate: '2017-02-04T00:00:00.000Z'
  },
  {
    tvshow: 'games',
    airdate: '2017-02-05T00:00:00.000Z'
  }
];

result = result.reduce( (acc, {tvshow, airdate}) => {
    acc.i += acc.airdate !== airdate;
    acc.airdate = airdate;
    acc.obj[`day${acc.i}`] = (acc.obj[`day${acc.i}`] || []).concat(tvshow);
    return acc;
}, {i:0, obj: {}} ).obj;

console.log(result);





我没有使用moment设置格式,这不是此代码中的决定因素。您可以添加它。

07-24 09:44
查看更多