如何按多个字段对值进行分组并按特定类型对其进行排序

我正在尝试根据特定字段(值)对对象数组进行分组,然后按特定类型对它们进行排序:


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

];

我正在尝试使用减速器来删除重复的内容,但我无法对国家/地区进行分组


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;

}, [])


哔哔one
浏览 216回答 3
3回答

动漫人物

你需要三张通行证按想要的键和值分组对数组进行排序更新prioritiey属性。function groupBy(data, key, ...values) {&nbsp; &nbsp; return Object.values(data.reduce((r, o) => {&nbsp; &nbsp; &nbsp; &nbsp; r[o[key]] = r[o[key]] || { ...o, ...Object.fromEntries(values.map(k => [k, []])) };&nbsp; &nbsp; &nbsp; &nbsp; values.forEach(k => r[o[key]][k].push(o[k]));&nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; }, {}));}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 }],&nbsp; &nbsp; result = groupBy(list, 'value', 'country')&nbsp; &nbsp; &nbsp; &nbsp; .sort(({ type: a }, { type: b }) => a > b || -(a < b))&nbsp; &nbsp; &nbsp; &nbsp; .map((priority => (o, i, { [i - 1]: p }) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (p && p.type === o.type) ++priority;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else priority = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return { ...o, priority };&nbsp; &nbsp; &nbsp; &nbsp; })());console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }

Cats萌萌

如果值不存在,您需要检查该值是否存在,否则将新值推送到国家/地区数组。var list = [&nbsp; {&nbsp; &nbsp; value: 'fox',&nbsp; &nbsp; country: 'nl',&nbsp; &nbsp; type: 'animal',&nbsp; &nbsp; priority: 1&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'fox',&nbsp; &nbsp; country: 'be',&nbsp; &nbsp; type: 'animal',&nbsp; &nbsp; priority: 2&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'cat',&nbsp; &nbsp; country: 'de',&nbsp; &nbsp; type: 'animal',&nbsp; &nbsp; priority: 3&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'david',&nbsp; country: 'nl',&nbsp; &nbsp; type: 'human',&nbsp; &nbsp; &nbsp;priority: 4&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'marc',&nbsp; &nbsp;country: 'be',&nbsp; &nbsp; type: 'human',&nbsp; &nbsp; &nbsp;priority: 5&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'lola',&nbsp; &nbsp;country: 'de',&nbsp; &nbsp; type: 'human',&nbsp; &nbsp; &nbsp;priority: 6&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'tiger',&nbsp; country: 'nl',&nbsp; &nbsp; type: 'animal',&nbsp; &nbsp; priority: 7&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'koala',&nbsp; country: 'be',&nbsp; &nbsp; type: 'animal',&nbsp; &nbsp; priority: 8&nbsp; },&nbsp; {&nbsp; &nbsp; value: 'tiger',&nbsp; country: 'nl',&nbsp; &nbsp; type: 'animal',&nbsp; &nbsp; priority: 9&nbsp; },];const l2 = list.reduce((accumulator, currentValue, index) => {&nbsp; const {&nbsp; &nbsp; country,&nbsp; &nbsp; ...value&nbsp; } = currentValue;&nbsp; const existing = accumulator&nbsp; &nbsp; .find(item => item.value === currentValue.value);&nbsp; if (existing) {&nbsp; &nbsp; const {&nbsp; &nbsp; &nbsp; countries&nbsp; &nbsp; } = existing;&nbsp; &nbsp; countries.push(country);&nbsp; &nbsp; existing.countries = Array.from(new Set(countries))&nbsp; } else {&nbsp; &nbsp; accumulator.push({ ...value,&nbsp; &nbsp; &nbsp; countries: [country]&nbsp; &nbsp; });&nbsp; }&nbsp; return accumulator;}, [])console.log(l2).as-console-wrapper {&nbsp; max-height: 100% !important;&nbsp; top: 0;}

慕桂英3389331

埃尔达的回答很好。只需在两次推送同一个国家之前添加国家检查。我还用实际的列表项替换了扩展运算符,以保留它们的结构顺序。const l2 = list.reduce((accumulator, currentValue) => {&nbsp; &nbsp; const {&nbsp; &nbsp; &nbsp; &nbsp; value,&nbsp; &nbsp; &nbsp; &nbsp; country,&nbsp; &nbsp; &nbsp; &nbsp; type,&nbsp; &nbsp; &nbsp; &nbsp; priority&nbsp; &nbsp; } = currentValue&nbsp; &nbsp; const existing = accumulator&nbsp; &nbsp; &nbsp; &nbsp; .find(item => item.value === currentValue.value)&nbsp; &nbsp; if (existing) {&nbsp; &nbsp; &nbsp; &nbsp; if (!(existing.countries.find(addedCountries => addedCountries === currentValue.country))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; existing.countries.push(currentValue.country)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; accumulator.push({ value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countries: [country],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; priority&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; return accumulator}, [])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript