function groupSortWords(params) {
let grouping = {},
afterGrouping = [];
params.forEach(item => {
let capital = item.word.replace(/\b[a-zA-Z]+\b/g, items => {
return items.charAt(0).toUpperCase();
});
if (grouping[capital]) {
grouping[capital].arr.push(item);
} else {
grouping[capital] = {
groupingName: `${capital}`,
arr: [item]
}
}
});
for (const key in grouping) {
if (Object.hasOwnProperty.call(grouping, key)) {
const itemKye = grouping[key];
afterGrouping.push(itemKye);
}
}
afterGrouping = afterGrouping.sort((a, b) => {
let x = a.groupingName,
y = b.groupingName;
return x > y ? 1 : x < y ? -1 : 0;
});
afterGrouping = afterGrouping.map(item => {
item.arr = item.arr.sort((a, b) => {
let x = a.word,
y = b.word;
return x > y ? 1 : x < y ? -1 : 0;
});
return item;
})
return afterGrouping
}
let words = [
{
id: 1652156,
word: "absolute",
},
{
id: 2365256,
word: "every",
},
{
id: 3156258,
word: "display",
},
{
id: 4695845,
word: "border",
},
{
id: 5125369,
word: "class",
},
{
id: 6985485,
word: "background",
},
{
id: 7156895,
word: "delete",
},
{
id: 8789651,
word: "color",
},
{
id: 9369529,
word: "application",
},
{
id: 1031562,
word: "length",
},
];
console.log(groupSortWords(words));
|