3 回答
TA贡献1797条经验 获得超6个赞
您可以通过date对相同组取消对象的排序、分组并获得平面数组。
const
questions = [{ _id: 1, q: 'why?', group: 'no-group', date: '8', selected: false }, { _id: 2, q: 'what?', group: 'group 1', date: '6', selected: false }, { _id: 3, q: 'when?', group: 'no-group', date: '7', selected: false }, { _id: 4, q: 'where?', group: 'group 1', date: '5', selected: false }, { _id: 5, q: 'which?', group: 'group 2', date: '3', selected: false }, { _id: 6, q: 'who?', group: 'no-group', date: '0', selected: false }, { _id: 7, q: 'why not?', group: 'group 2', date: '9', selected: false }, { _id: 8, q: 'who, me?', group: 'group 1', date: '4', selected: false }, { _id: 9, q: 'where is waldo?', group: 'group 1', date: '1', selected: false }, { _id: 10, q: 'which way is up?', group: 'no-group', date: '2', selected: false }, { _id: 11, q: 'when is lunch?', group: 'group 2', date: '10', selected: false }],
result = questions
.sort((a, b) => a.date - b.date)
.map((groups => o => {
if (o.group === 'no-group') return o;
if (groups[o.group]) {
groups[o.group].push(o);
return [];
}
return groups[o.group] = [o];
})({}))
.flat();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
TA贡献1836条经验 获得超4个赞
sort
数组由date
reduce
带有累加器的数组Map
。如果它是一个项目,请使用唯一的键
no-group
添加一个新条目。Map
_id
如果它是一个有效的项目
group
,则使用group
作为键和具有相同的项目数组group
。每个都
no-group
将作为键添加到 Map 中。其余项目将根据该项目的第一个条目添加group
。获取
values
地图并将其击倒
const questions = [{ _id: 1, q: 'why?', group: 'no-group', date: '8', selected: false }, { _id: 2, q: 'what?', group: 'group 1', date: '6', selected: false }, { _id: 3, q: 'when?', group: 'no-group', date: '7', selected: false }, { _id: 4, q: 'where?', group: 'group 1', date: '5', selected: false }, { _id: 5, q: 'which?', group: 'group 2', date: '3', selected: false }, { _id: 6, q: 'who?', group: 'no-group', date: '0', selected: false }, { _id: 7, q: 'why not?', group: 'group 2', date: '9', selected: false }, { _id: 8, q: 'who, me?', group: 'group 1', date: '4', selected: false }, { _id: 9, q: 'where is waldo?', group: 'group 1', date: '1', selected: false }, { _id: 10, q: 'which way is up?', group: 'no-group', date: '2', selected: false }, { _id: 11, q: 'when is lunch?', group: 'group 2', date: '10', selected: false }]
const group = questions.sort((a, b) => a.date - b.date)
.reduce((map, o) =>
o.group === 'no-group'
? map.set(o._id, o)
: map.set(o.group, [...map.get(o.group) || [], o] )
, new Map)
const output = Array.from(group.values()).flat()
console.log( output )
TA贡献1829条经验 获得超6个赞
我们可以date先对它进行排序,然后对对象进行分组,group并将组保存在一个对象中,同时保存组的索引。
然后我们最终可以将组插入到正确的索引处以获得最终的数组:
const
groups = [{ _id: 1, q: 'why?', group: 'no-group', date: '8', selected: false }, { _id: 2, q: 'what?', group: 'group 1', date: '6', selected: false }, { _id: 3, q: 'when?', group: 'no-group', date: '7', selected: false }, { _id: 4, q: 'where?', group: 'group 1', date: '5', selected: false }, { _id: 5, q: 'which?', group: 'group 2', date: '3', selected: false }, { _id: 6, q: 'who?', group: 'no-group', date: '0', selected: false }, { _id: 7, q: 'why not?', group: 'group 2', date: '9', selected: false }, { _id: 8, q: 'who, me?', group: 'group 1', date: '4', selected: false }, { _id: 9, q: 'where is waldo?', group: 'group 1', date: '1', selected: false }, { _id: 10, q: 'which way is up?', group: 'no-group', date: '2', selected: false }, { _id: 11, q: 'when is lunch?', group: 'group 2', date: '10', selected: false }];
const sort = (groups) => {
const groupSorted = groups.sort((a, b) => a.date - b.date)
.reduce((r, o, i) => {
if (o.group !== 'no-group') {
r[o.group] = (r[o.group] || []).concat(o);
//insert the index of the group
r.index[o.group] = r.index[o.group] || i;
} else {
r.res.push(o)
}
return r;
}, {
index: {},
res: []
});
Object.entries(groupSorted.index).forEach(([group, idx]) => {
groupSorted.res.splice(idx, 0, groupSorted[group])
});
return groupSorted.res.flat();
}
console.log(sort(groups))
添加回答
举报