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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用復(fù)雜/動態(tài)條件(升序和降序)對包含對象的數(shù)組進行排序?

使用復(fù)雜/動態(tài)條件(升序和降序)對包含對象的數(shù)組進行排序?

慕碼人2483693 2022-08-04 17:21:00
我們有一個包含數(shù)據(jù)的數(shù)組,我想按鍵對它進行排序:users如果有1個對象,我按其屬性排序。usersname如果有多個條目,我按 排序。usersusers.length例:DESCENDING: Zorya, Gorya, Dorya, Borya, Aorya, 4, 2, 0  ASCENDING: Aorya, Borya, Dorya, Gorya, Zorya, 2, 4, 0這是我到目前為止所做的:const array = [{  name: "qw",  users: [    { name: "Borya" },  ],}, {  name: "qw",  users: [    { name: "Gorya" },  ],}, {  name: "qw",  users: [   { name: "Zorya" }, ]}, {  name: "qw",  users: [    { name: "Var" },    { name: "Var2" },  ],}, {  name: "qw",  users: [],}, {  name: "qw",  users: [    { name: "Aorya" },  ],}, {  name: "qw",  users: [    { name: "rwerwerwe" },    { name: "tregdf" },    { name: "gdfgdf" },    { name: "Vayetrtertrr2" },  ]}, {  name: "qw",  users: [   { name: "Dorya" },  ],}];function orderCustomBy(collection, key, direction) {  const direct = direction === "desc" ? -1 : 1;    let compare = (a, b) => {    if (a === null) return -1;    if (b === null) return 1;    // Just commenting this out because there's no `intlCollator` in here:    // return intlCollator.compare(a, b);  };    if (key === "users") {    compare = (a, b) => {      // What should go in here?      // intlCollator.compare(a[0].name, b[0].name);            return 1;    };  }    return [].concat(collection).sort((a, b) => {    const result = compare(a[key], b[key]);    return result * direct;  });}console.log(orderCustomBy(array, 'users', 'asc')  .map(item => item.users.length === 1 ? item.users[0].name : item.users.length));  console.log(orderCustomBy(array, 'users', 'desc')  .map(item => item.users.length === 1 ? item.users[0].name : item.users.length));.as-console-wrapper {  max-height: 100% !important;}
查看完整描述

2 回答

?
www說

TA貢獻1775條經(jīng)驗 獲得超8個贊

在以下情況下,您基本上需要考慮函數(shù)中的一些不同組合:comparekey === 'users

  • 兩者都有一個用戶,因此我們比較。user[0].name

  • 只有一個用戶,所以在 .aab

  • 只有一個用戶,所以在 .bba

  • 沒有一個有單個用戶,所以我們只是比較。users.length

它看起來像這樣:

if (a.users.length === 1 && b.users.length === 1) {

  // If both have a single user, sort by users[0].name:

  return a.users[0].name.localeCompare(b.users[0].name);

} else if (a.users.length === 1) {

  // If only `a` has a single user, `a` goes before `b`:

  return -1;

} else if (b.users.length === 1) {

  // If only `b` has a single user, `b` goes before `a`:

  return 1;

}


// Otherwise, sort by users.length:

return a.users.length - b.users.length;

在這里,您可以看到它的實際效果:

const array = [{

  name: "qw",

  users: [

    { name: "Borya" },

  ],

}, {

  name: "qw",

  users: [

    { name: "Gorya" },

  ],

}, {

  name: "qw",

  users: [

   { name: "Zorya" },

 ]

}, {

  name: "qw",

  users: [

    { name: "Var" },

    { name: "Var2" },

  ],

}, {

  name: "qw",

  users: [],

}, {

  name: "qw",

  users: [

    { name: "Aorya" },

  ],

}, {

  name: "qw",

  users: [

    { name: "rwerwerwe" },

    { name: "tregdf" },

    { name: "gdfgdf" },

    { name: "Vayetrtertrr2" },

  ]

}, {

  name: "qw",

  users: [

   { name: "Dorya" },

  ],

}];


function orderCustomBy(collection, key, direction) {

  const direct = direction === "desc" ? -1 : 1;

  

  let compare;

  

  if (key === "users") {

    compare = (a, b) => {

      if (a.users.length === 1 && b.users.length === 1) {

        // If both have a single user, sort by users[0].name:

        return a.users[0].name.localeCompare(b.users[0].name);

      } else if (a.users.length === 1) {

        // If only `a` has a single user, `a` goes before `b`:

        return -1;

      } else if (b.users.length === 1) {

        // If only `b` has a single user, `b` goes before `a`:

        return 1;

      }

      

      // Otherwise, sort by users.length:

      return a.users.length - b.users.length;

    };

  } else {

    compare = (a, b) => {

      if (a === null) return -1;

      if (b === null) return 1;

      // Just commenting this out because there's no `intlCollator` in here:

      // return intlCollator.compare(a, b);

    };

  }

  

  return [].concat(collection).sort((a, b) => compare(a, b) * direct);

}


console.log(orderCustomBy(array, 'users', 'asc')

  .map(item => item.users.length === 1 ? item.users[0].name : item.users.length));

  

console.log(orderCustomBy(array, 'users', 'desc')

  .map(item => item.users.length === 1 ? item.users[0].name : item.users.length));

.as-console-wrapper {

  max-height: 100% !important;

}


查看完整回答
反對 回復(fù) 2022-08-04
?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

由于這些排序標(biāo)準(zhǔn)很復(fù)雜,因此最好首先將復(fù)雜數(shù)組轉(zhuǎn)換為更易于推理和排序的更簡單數(shù)組。


下面的函數(shù)可以做到這一點,生成這個未排序的數(shù)組:simplifyArray()


[ "Borya", "Gorya", "Zorya", "2", "0", "Aorya", "4", "Dorya" ]

排序它應(yīng)該很簡單。


function simplifyArray (arr) {

  return arr.map(el => {

    let length = el.users.length;

    if (length === 1) { return el.users[0].name; }

    else return String(length);

  });

}


const array = [

{

    name: "qw",

    users: [

      { name: "Borya" }

    ]

  },

  {

    name: "qw",

    users: [

      { name: "Gorya" }

    ]

  },

  {

    name: "qw",

    users: [

     { name: "Zorya" }

   ]

  },

  {

    name: "qw",

    users: [

      { name: "Var" },

      { name: "Var2" }

    ]

  },

  {

    name: "qw",

    users: []

  },

  {

    name: "qw",

    users: [

      { name: "Aorya" }

    ]

  },

  {

    name: "qw",

    users: [

      { name: "rwerwerwe" },

      { name: "tregdf" },

      { name: "gdfgdf" },

      { name: "Vayetrtertrr2" }

    ]

  },

  {

    name: "qw",

    users: [

     { name: "Dorya" }

    ]

  },

];


const simplerArray = simplifyArray(array);


console.log(simplerArray);


查看完整回答
反對 回復(fù) 2022-08-04
  • 2 回答
  • 0 關(guān)注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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