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

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

如何按多個(gè)字段對(duì)值進(jìn)行分組并按特定類型對(duì)其進(jìn)行排序

如何按多個(gè)字段對(duì)值進(jìn)行分組并按特定類型對(duì)其進(jìn)行排序

嗶嗶one 2022-06-09 16:48:11
我正在嘗試根據(jù)特定字段(值)對(duì)對(duì)象數(shù)組進(jìn)行分組,然后按特定類型對(duì)它們進(jìn)行排序:var list = [    {value: 'fox', country: 'nl', type: 'animal', priority: 1},  {value: 'fox', country: 'be', type: 'animal', priority: 2},  {value: 'cat', country: 'de', type: 'animal', priority: 3},  {value: 'david', country: 'nl', type: 'human', priority: 4},  {value: 'marc', country: 'be', type: 'human', priority: 5},  {value: 'lola', country: 'de', type: 'human', priority: 6},  {value: 'tiger', country: 'nl', type: 'animal', priority: 7},  {value: 'koala', country: 'be', type: 'animal', priority: 8},  {value: 'tiger', country: 'nl', type: 'animal', priority: 9},];// output/result should be:[  {value: 'fox', countries: ['nl', 'be'], type: 'animal', priority: 1},  {value: 'cat', countries: ['de'], type: 'animal', priority: 2},  {value: 'tiger', countries: ['nl'], type: 'animal', priority: 3},  {value: 'koala', contries: ['be'], type: 'animal', priority: 4},  {value: 'david', contries: ['nl'], type: 'human', priority: 1},  {value: 'marc', contries: ['be'], type: 'human', priority: 2},  {value: 'lola', contries: ['de'], type: 'human', priority: 3},];我正在嘗試使用減速器來(lái)刪除重復(fù)的內(nèi)容,但我無(wú)法對(duì)國(guó)家/地區(qū)進(jìn)行分組list.reduce((accumulator, currentValue, index) => {    const {country, value} = currentValue;  if(!accumulator.some(item => item.value === currentValue.value)) {     accumulator.push({      value: currentValue.value,      countries: [accumulator.country].concat(currentValue.country)    })  }  return accumulator;}, [])
查看完整描述

3 回答

?
動(dòng)漫人物

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

你需要三張通行證

  • 按想要的鍵和值分組

  • 對(duì)數(shù)組進(jìn)行排序

  • 更新prioritiey屬性。

function groupBy(data, key, ...values) {

    return Object.values(data.reduce((r, o) => {

        r[o[key]] = r[o[key]] || { ...o, ...Object.fromEntries(values.map(k => [k, []])) };

        values.forEach(k => r[o[key]][k].push(o[k]));

        return r;

    }, {}));

}


var list = [{ value: 'fox', country: 'nl', type: 'animal', priority: 1 }, { value: 'fox', country: 'be', type: 'animal', priority: 2 }, { value: 'cat', country: 'de', type: 'animal', priority: 3 }, { value: 'david', country: 'nl', type: 'human', priority: 4 }, { value: 'marc', country: 'be', type: 'human', priority: 5 }, { value: 'lola', country: 'de', type: 'human', priority: 6 }, { value: 'tiger', country: 'nl', type: 'animal', priority: 7 }, { value: 'koala', country: 'be', type: 'animal', priority: 8 }, { value: 'tiger', country: 'nl', type: 'animal', priority: 9 }],

    result = groupBy(list, 'value', 'country')

        .sort(({ type: a }, { type: b }) => a > b || -(a < b))

        .map((priority => (o, i, { [i - 1]: p }) => {

            if (p && p.type === o.type) ++priority;

            else priority = 1;

            return { ...o, priority };

        })());


console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對(duì) 回復(fù) 2022-06-09
?
Cats萌萌

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

如果值不存在,您需要檢查該值是否存在,否則將新值推送到國(guó)家/地區(qū)數(shù)組。


var list = [

  {    value: 'fox',    country: 'nl',    type: 'animal',    priority: 1  },

  {    value: 'fox',    country: 'be',    type: 'animal',    priority: 2  },

  {    value: 'cat',    country: 'de',    type: 'animal',    priority: 3  },

  {    value: 'david',  country: 'nl',    type: 'human',     priority: 4  },

  {    value: 'marc',   country: 'be',    type: 'human',     priority: 5  },

  {    value: 'lola',   country: 'de',    type: 'human',     priority: 6  },

  {    value: 'tiger',  country: 'nl',    type: 'animal',    priority: 7  },

  {    value: 'koala',  country: 'be',    type: 'animal',    priority: 8  },

  {    value: 'tiger',  country: 'nl',    type: 'animal',    priority: 9  },

];


const l2 = list.reduce((accumulator, currentValue, index) => {

  const {

    country,

    ...value

  } = currentValue;

  const existing = accumulator

    .find(item => item.value === currentValue.value);

  if (existing) {

    const {

      countries

    } = existing;

    countries.push(country);

    existing.countries = Array.from(new Set(countries))

  } else {

    accumulator.push({ ...value,

      countries: [country]

    });

  }

  return accumulator;

}, [])


console.log(l2)

.as-console-wrapper {

  max-height: 100% !important;

  top: 0;

}


查看完整回答
反對(duì) 回復(fù) 2022-06-09
?
慕桂英3389331

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

埃爾達(dá)的回答很好。只需在兩次推送同一個(gè)國(guó)家之前添加國(guó)家檢查。


我還用實(shí)際的列表項(xiàng)替換了擴(kuò)展運(yùn)算符,以保留它們的結(jié)構(gòu)順序。


const l2 = list.reduce((accumulator, currentValue) => {

    const {

        value,

        country,

        type,

        priority

    } = currentValue

    const existing = accumulator

        .find(item => item.value === currentValue.value)

    if (existing) {

        if (!(existing.countries.find(addedCountries => addedCountries === currentValue.country))) {

            existing.countries.push(currentValue.country)

        }

    } else {

        accumulator.push({ value,

            countries: [country],

            type,

            priority

        })

    }

    return accumulator

}, [])


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

添加回答

舉報(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)