为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用两个属性对对象数组进行排序,但根据条件按排序顺序有条件地分组?

如何使用两个属性对对象数组进行排序,但根据条件按排序顺序有条件地分组?

精慕HU 2023-11-11 21:47:54
这个问题是这个问题的延续。假设我有一个如下所示的数组: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 }, ];如何编写代码对其进行排序,以便按日期和组对对象进行排序。'no-group'但是,如果:假设组 1 作为第二个对象出现,则以下对象应为按日期排序的组 1,直到列出所有组 1 对象,并且对于除具有组属性值的对象之外的所有其他对象都相同。因此,对于上面的数组,我应该得到如下输出:[  {_id: 6, q: 'who?', group: 'no-group', date: '0', selected: false },  {_id: 9, q: 'where is waldo?', group: 'group 1', date: '1', selected: false },   {_id: 8, q: 'who, me?', group: 'group 1', date: '4', selected: false },   {_id: 4, q: 'where?', group: 'group 1', date: '5', selected: false },   {_id: 2, q: 'what?', group: 'group 1', date: '6', selected: false },  {_id: 10, q: 'which way is up?', group: 'no-group', date: '2', selected: false },  {_id: 5, q: 'which?', group: 'group 2', date: '3', selected: false },  {_id: 7, q: 'why not?', group: 'group 2', date: '9', selected: false },   {_id: 11, q: 'when is lunch?', group: 'group 2', date: '10', selected: false },  {_id: 3, q: 'when?', group: 'no-group', date: '7', selected: false },   {_id: 1, q: 'why?', group: 'no-group', date: '8', selected: false }, ];// the spacing is just for easier readability.
查看完整描述

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; }


查看完整回答
反对 回复 2023-11-11
?
HUH函数

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 )


查看完整回答
反对 回复 2023-11-11
?
肥皂起泡泡

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))


查看完整回答
反对 回复 2023-11-11
  • 3 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信