第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用兩個(gè)屬性對(duì)對(duì)象數(shù)組進(jìn)行排序,但根據(jù)條件按排序順序有條件地分組?

如何使用兩個(gè)屬性對(duì)對(duì)象數(shù)組進(jìn)行排序,但根據(jù)條件按排序順序有條件地分組?

精慕HU 2023-11-11 21:47:54
這個(gè)問題是這個(gè)問題的延續(xù)。假設(shè)我有一個(gè)如下所示的數(shù)組: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 }, ];如何編寫代碼對(duì)其進(jìn)行排序,以便按日期和組對(duì)對(duì)象進(jìn)行排序。'no-group'但是,如果:假設(shè)組 1 作為第二個(gè)對(duì)象出現(xiàn),則以下對(duì)象應(yīng)為按日期排序的組 1,直到列出所有組 1 對(duì)象,并且對(duì)于除具有組屬性值的對(duì)象之外的所有其他對(duì)象都相同。因此,對(duì)于上面的數(shù)組,我應(yīng)該得到如下輸出:[  {_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貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以通過date對(duì)相同組取消對(duì)象的排序、分組并獲得平面數(shù)組。


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


查看完整回答
反對(duì) 回復(fù) 2023-11-11
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

  • sort數(shù)組由date

  • reduce帶有累加器的數(shù)組Map。

  • 如果它是一個(gè)項(xiàng)目,請(qǐng)使用唯一的鍵no-group添加一個(gè)新條目。Map_id

  • 如果它是一個(gè)有效的項(xiàng)目group,則使用group作為鍵和具有相同的項(xiàng)目數(shù)組group。

  • 每個(gè)都no-group將作為鍵添加到 Map 中。其余項(xiàng)目將根據(jù)該項(xiàng)目的第一個(gè)條目添加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 )


查看完整回答
反對(duì) 回復(fù) 2023-11-11
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

我們可以date先對(duì)它進(jìn)行排序,然后對(duì)對(duì)象進(jìn)行分組,group并將組保存在一個(gè)對(duì)象中,同時(shí)保存組的索引。


然后我們最終可以將組插入到正確的索引處以獲得最終的數(shù)組:


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


查看完整回答
反對(duì) 回復(fù) 2023-11-11
  • 3 回答
  • 0 關(guān)注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)