所以我有這個物件陣列,它由專案組成,其中一個專案有 6 種組合作為物件。
假設我有這個物件的一個??組合:
{ "1": "ab", "2": "bc", "3": "ac" }
該陣列還包含 5 個其他物件,這些物件具有該物件的不同組合。像這樣:
{ "1": "ab", "2": "ac", "3": "bc" }
{ "1": "ac", "2": "bc", "3": "ab" }
{ "1": "ac", "2": "ab", "3": "bc" }
{ "1": "bc", "2": "ac", "3": "ab" }
{ "1": "bc", "2": "ab", "3": "ac" }
這樣我有大約 1000 個專案,每個專案有 6 個組合在一個陣列中,我需要過濾并只回傳每個專案的 1 個組合。
所以假設這是兩個專案的 12 種不同組合的陣列:
[
{ "1": "ab", "2": "bc", "3": "ac" }
{ "1": "ab", "2": "ac", "3": "bc" }
{ "1": "ac", "2": "bc", "3": "ab" }
{ "1": "ac", "2": "ab", "3": "bc" }
{ "1": "bc", "2": "ac", "3": "ab" }
{ "1": "bc", "2": "ab", "3": "ac" }
{ "1": "de", "2": "ef", "3": "df" }
{ "1": "df", "2": "ef", "3": "de" }
{ "1": "de", "2": "df", "3": "ef" }
{ "1": "ef", "2": "df", "3": "de" }
{ "1": "df", "2": "de", "3": "ef" }
{ "1": "ef", "2": "de", "3": "df" }
]
我想過濾該陣列并僅回傳每個陣列的 1 個組合,如下所示:
[
{ "1": "ab", "2": "bc", "3": "ac" }
{ "1": "de", "2": "ef", "3": "df" }
]
請注意,第一個物件中的“ab”可以是完全不同組合的值,例如:
{ "1": "ab", "2": "df", "3": "fe" }
我怎樣才能有效地做到這一點并考慮到良好的性能?
uj5u.com熱心網友回復:
如果值始終是字串,您可以Object.values()根據每個元素的排序創建一個復合鍵,然后使用簡單的“分組依據”操作,這里使用reduce()累積到Map。
我只在此處設定第一個匹配項(僅當地圖沒有匹配條目時),它將回傳第一個匹配項,如果您想要最后一個匹配項,只需在每次迭代時設定鍵。
const input = [
{ 1: 'ab', 2: 'bc', 3: 'ac' },
{ 1: 'ab', 2: 'ac', 3: 'bc' },
{ 1: 'ac', 2: 'bc', 3: 'ab' },
{ 1: 'ac', 2: 'ab', 3: 'bc' },
{ 1: 'bc', 2: 'ac', 3: 'ab' },
{ 1: 'bc', 2: 'ab', 3: 'ac' },
{ 1: 'de', 2: 'ef', 3: 'df' },
{ 1: 'df', 2: 'ef', 3: 'de' },
{ 1: 'de', 2: 'df', 3: 'ef' },
{ 1: 'ef', 2: 'df', 3: 'de' },
{ 1: 'df', 2: 'de', 3: 'ef' },
{ 1: 'ef', 2: 'de', 3: 'df' },
];
const result = [
...input
.reduce((a, o) => {
const key = Object.values(o)
.sort((a, b) => a.localeCompare(b))
.join('_');
if (!a.has(key)) a.set(key, o);
return a;
}, new Map())
.values(),
];
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果您不介意回傳最后一個匹配的組合,您可以將其簡化為一個map()內部new Map()呼叫。
const input = [{ 1: 'ab', 2: 'bc', 3: 'ac' }, { 1: 'ab', 2: 'ac', 3: 'bc' }, { 1: 'ac', 2: 'bc', 3: 'ab' }, { 1: 'ac', 2: 'ab', 3: 'bc' }, { 1: 'bc', 2: 'ac', 3: 'ab' }, { 1: 'bc', 2: 'ab', 3: 'ac' }, { 1: 'de', 2: 'ef', 3: 'df' }, { 1: 'df', 2: 'ef', 3: 'de' }, { 1: 'de', 2: 'df', 3: 'ef' }, { 1: 'ef', 2: 'df', 3: 'de' }, { 1: 'df', 2: 'de', 3: 'ef' }, { 1: 'ef', 2: 'de', 3: 'df' },];
const result = [
...new Map(
input.map((o) => [
Object.values(o)
.sort((a, b) => a.localeCompare(b))
.join('_'),
o,
])
).values(),
];
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
這是一個關于如何對過濾器回傳的結果陣列進行排序的快速示例。它只是一個接受比較函式的函式,Object.values對傳遞的組合物件進行排序,然后從排序的值陣列重建物件(這里使用Object.fromEntries(),但如果您不介意 0 索引物件,您可以使用Object.assign()并放棄額外的map(),即。return Object.assign({}, Object.values(obj).sort(compare));)
const input = [
{ 1: 'ab', 2: 'bc', 3: 'ac' },
{ 1: 'ab', 2: 'ac', 3: 'bc' },
{ 1: 'ac', 2: 'bc', 3: 'ab' },
{ 1: 'ac', 2: 'ab', 3: 'bc' },
{ 1: 'bc', 2: 'ac', 3: 'ab' },
{ 1: 'bc', 2: 'ab', 3: 'ac' },
{ 1: 'de', 2: 'ef', 3: 'dc' },
{ 1: 'dc', 2: 'ef', 3: 'de' },
{ 1: 'de', 2: 'dc', 3: 'ef' },
{ 1: 'ef', 2: 'dc', 3: 'de' },
{ 1: 'dc', 2: 'de', 3: 'ef' },
{ 1: 'ef', 2: 'de', 3: 'dc' },
];
const result = [...new Map(input.map((o) => [Object.values(o).sort((a, b) => a.localeCompare(b)).join('_'), o,])).values(),];
console.log('Initial result: ', result);
function sort_combination(obj, compare = (a, b) => a.localeCompare(b)) {
return Object.fromEntries(
Object.values(obj)
.sort(compare)
.map((v, i) => [i 1, v])
);
}
const includes_c = (a, b) => b.includes('c') - a.includes('c') || a.localeCompare(b);
const sorted = result.map((o) => sort_combination(o, includes_c));
console.log('Sorted result: ', sorted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346636.html
標籤:javascript 数组 筛选
上一篇:如何從陣列中洗掉陣列?
