我知道如何進行基本的排序和分組,但這是我找不到正確邏輯的事情。
const test = [
{name: 'a', type: '1'},
{name: 'a', type: '2'},
{name: 'a', type: '3'},
{name: 'b', type: '1'},
{name: 'b', type: '2'},
{name: 'c', type: '3'},
{name: 'd', type: '1'},
{name: 'e', type: '4'},
{name: 'e', type: '3'},
{name: 'f', type: '1'},
{name: 'f', type: '2'},
{name: 'f', type: '3'},
{name: 'g', type: '2'},
];
在我擁有的陣列之上和我想要得到的結果之下。
const clean = [
{type: '1-2-3', names: 'a'},
{type: '1-2', names: 'b'},
{type: '3', names: 'c'},
{type: '3-4', names: 'e'},
{type: '1', names: 'd'},
];
我有一個包含許多需要分組的雙重名稱的檔案。我需要一個具有唯一組和名稱的物件。
讓我們說名字a,b,c。a 有組 1,2,3 - b 有 2,3 c 有 1,3 我們創建 3 個組,groupName '1-2-3'。, '2-3', '1-3' 并添加適合組內的名稱,因此我們沒有'雙重名稱
我希望你們明白我的意思。請幫助我使用什么登錄名進行排序/分組
uj5u.com熱心網友回復:
您可以簡單地使用reduce, 來執行分組。像這樣:
const test = [
{name: 'a', type: '1'},
{name: 'a', type: '2'},
{name: 'a', type: '3'},
{name: 'b', type: '1'},
{name: 'b', type: '2'},
{name: 'c', type: '3'},
{name: 'd', type: '1'},
{name: 'e', type: '4'},
{name: 'e', type: '3'},
{name: 'f', type: '1'},
{name: 'f', type: '2'},
{name: 'f', type: '3'},
{name: 'g', type: '2'},
];
const response = test.sort((a, b) => {
return a.type - b.type;
}).reduce((agg, current) => {
if(agg[current.name]) {
agg[current.name] = `${agg[current.name]}-${current.type}`
} else {
agg[current.name] = current.type;
}
return agg
}, {});
const groupedData = {};
Object.entries(response).forEach(([k, v]) => {
if(groupedData[v]) {
groupedData[v].push(k);
} else {
groupedData[v] = [k];
}
})
const output = [];
Object.entries(groupedData).forEach(([k, v]) => {
output.push({type: k, names: v})
})
console.log(output);
uj5u.com熱心網友回復:
讓我們逐步了解它。我們需要按 對專案進行分組name。我們怎么能這樣做?
我們可以有一個映射/物件,其鍵為name,值為陣列。如果name映射/物件中不存在鍵,我們可以使用陣列對其進行初始化。否則,如果是這樣,我們可以將下一個型別推送給它。
const groups_in_array = {};
for (const item of test) {
const { name, type } = item;
if (!(name in groups_in_array)) { // Group doesn't exist yet. Initialize it with an array.
groups_in_array[name] = [type];
} else {
groups_in_array[name].push(type); // Group already exists. Push the type to it.
}
}
name接下來,我們可以創建一個包含所有鍵s的陣列,groups_in_array并使用 連接所有型別-。
const groups = [];
for (const group in groups_in_array) {
groups.push({
name: group,
type: groups_in_array[group].join("-")
});
}
把它們放在一起。
const test = [
{name: 'a', type: '1'},
{name: 'a', type: '2'},
{name: 'a', type: '3'},
{name: 'b', type: '1'},
{name: 'b', type: '2'},
{name: 'c', type: '3'},
{name: 'd', type: '1'},
{name: 'e', type: '4'},
{name: 'e', type: '3'},
];
const groups_in_array = {};
for (const item of test) {
const { name, type } = item;
if (!(name in groups_in_array)) {
groups_in_array[name] = [type];
} else {
groups_in_array[name].push(type);
}
}
const groups = [];
for (const group in groups_in_array) {
groups.push({
name: group,
type: groups_in_array[group].join("-")
});
}
console.log(groups);
請注意,默認情況下物件/地圖未排序(排序)。
uj5u.com熱心網友回復:
無需過度邏輯
const test = [
{name: 'a', type: '1'},
{name: 'a', type: '2'},
{name: 'a', type: '3'},
{name: 'b', type: '1'},
{name: 'b', type: '2'},
{name: 'c', type: '3'},
{name: 'd', type: '1'},
{name: 'e', type: '4'},
{name: 'e', type: '3'},
];
const result = test
.sort((a, b) => a.name - b.name)
.reduce((previous, {name, type}) => {
const group = previous.findIndex(item => item.names === name);
if (group > -1) {
previous[group].type = `-${type}`;
previous[group].type = previous[group].type.split('-').sort((a, b) => a - b).join('-')
} else {
previous.push({type, names: name});
}
return previous;
}, []);
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/504364.html
標籤:javascript
