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

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

根據(jù)主對象內(nèi)另一個對象列表的屬性值對復(fù)雜的 javascript 對象進(jìn)行排序

根據(jù)主對象內(nèi)另一個對象列表的屬性值對復(fù)雜的 javascript 對象進(jìn)行排序

滄海一幻覺 2023-01-06 15:53:02
我有以下結(jié)構(gòu)中的對象列表,這些對象已經(jīng)按頂層的名稱屬性排序。 [{     name: 'name1'     team: 'team1'     statuses: [{ time: 'day1', color: 'green', message: 'looks good'}, { time: 'day2', color: 'green', message: 'looks good'}]    },    {     name: 'name2'     team: 'team2'     statuses: [{ time: 'day1', color: 'yellow', message: 'mild concern'}, { time: 'day2', color: 'red', message: 'critical issue'}]    },    {     name: 'name3'     team: 'team3'     statuses: [{ time: 'day1', color: 'orange', message: 'mild concern'}, { time: 'day2', color: 'orange', message: 'potential issue'}]    }]上面的列表應(yīng)該根據(jù)狀態(tài)列表中最后一個對象的顏色屬性使用自定義排序順序(紅色、橙色、綠色)進(jìn)行排序。預(yù)期列表包含此順序的對象 team2、team3、team1,如果有多個相同顏色,則它應(yīng)該在頂層保留名稱屬性的排序。我嘗試按以下方式使用 reduce 函數(shù)并將它們組合在一起,但沒有得到預(yù)期的輸出。 teams.reduce((r, t) => {     if(t.statuses[1].color === 'red');       r.push(t)    return r;   }, { [] })     teams.reduce((r, t) => {     if(t.statuses[1].color === 'orange');       r.push(t)    return r;   }, { [] })   teams.reduce((r, t) => {     if(t.statuses[1].color === 'green');       r.push(t)    return r;   }, { [] })
查看完整描述

2 回答

?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗 獲得超7個贊

在原始數(shù)組上使用過濾器,為了排序順序,我使用了 COLORS 數(shù)組。我在最后添加了顏色“黃色”,因為它在排序標(biāo)準(zhǔn)中沒有提到,您可以根據(jù)自己的選擇進(jìn)行處理。


擴(kuò)展:


正如所希望的那樣,黃色現(xiàn)在介于橙色和黃色之間。

如果它是綠色的并且評論不是“看起來不錯”那么它應(yīng)該出現(xiàn)在開頭。

let list = [{

     name: 'name1',

     team: 'team1',

     statuses: [{ time: 'day1', color: 'green', message: 'looks good'}, { time: 'day2', color: 'green', message: 'looks good'}]

    },

    {

     name: 'name2',

     team: 'team2',

     statuses: [{ time: 'day1', color: 'yellow', message: 'mild concern'}, { time: 'day2', color: 'red', message: 'critical issue'}]

    },

    {

     name: 'name3',

     team: 'team3',

     statuses: [{ time: 'day1', color: 'orange', message: 'mild concern'}, { time: 'day2', color: 'orange', message: 'potential issue'}]

    },

    {

     name: 'name4',

     team: 'team4',

     statuses: [{ time: 'day1', color: 'yellow', message: 'mild concern'}, { time: 'day2', color: 'green', message: 'potential issue'}]

    }

    ];


const COLORS = ['red', 'orange', 'yellow', 'green'];

const GREEN = COLORS.indexOf('green');

 

let result = list.sort((a,b) => {

    let stata = a.statuses[a.statuses.length-1];

    let statb = b.statuses[b.statuses.length-1];

    let cola = COLORS.indexOf(stata.color);

    let colb = COLORS.indexOf(statb.color);

    if (cola == GREEN && stata.message != 'looks good') {

        return (colb == GREEN && statb.message != 'looks good') ? a.name.localeCompare(b.name) : -1;

    }

    if  (colb == GREEN && statb.message != 'looks good') {

        return 1;

    }

    return (cola < colb) ? -1 : ((cola > colb) ? 1: a.name.localeCompare(b.name));

});

 

console.log(result);


查看完整回答
反對 回復(fù) 2023-01-06
?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗 獲得超7個贊

您可以創(chuàng)建一個對象,在其中定義顏色順序,然后使用sort首先按顏色排序的方法,如果顏色相同,則按名稱排序


const data = [{"name":"name1","team":"team1","statuses":[{"time":"day1","color":"green","message":"looks good"},{"time":"day2","color":"green","message":"looks good"}]},{"name":"name2","team":"team2","statuses":[{"time":"day1","color":"yellow","message":"mild concern"},{"time":"day2","color":"red","message":"critical issue"}]},{"name":"name3","team":"team3","statuses":[{"time":"day1","color":"orange","message":"mild concern"},{"time":"day2","color":"orange","message":"potential issue"}]}]


const order = {

  red: 1,

  orange: 2,

  green: 3

}


data.sort((a, b) => {

  const aColor = a.statuses.slice(-1)[0].color;

  const bColor = b.statuses.slice(-1)[0].color;

  return order[aColor] - order[bColor] || a.name.localeCompare(b.name)

})


console.log(data)


查看完整回答
反對 回復(fù) 2023-01-06
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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