3 回答

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

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

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
}, [])
添加回答
舉報(bào)