gpt4 book ai didi

javascript - 对象/数组操作

转载 作者:行者123 更新时间:2023-12-03 05:00:15 26 4
gpt4 key购买 nike

我从数据库查询(nodejs,使用带有 Knex 的 Postgres)获取一个对象数组,如下所示:(缩小版)

[ 
{
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
}
]

返回此值的查询将播放日期字段限制为5天。这意味着无论我用它做什么,它总是有 5 天

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

预期输出示例

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

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

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 的格式,这不是此代码中的决定因素。您可以添加它。

关于javascript - 对象/数组操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com