[英]array.groupBy in TypeScript
The basic array class has .map
, .forEach
, .filter
, and .reduce
, but .groupBy
i noticably absent, preventing me from doing something like基本数组类有
.map
、 .forEach
、 .filter
和.reduce
,但.groupBy
我明显缺席,这让我无法做类似的事情
const MyComponent = (props:any) => {
return (
<div>
{
props.tags
.groupBy((t)=>t.category_name)
.map((group)=>{
[...]
})
}
</div>
)
}
I ended up implementing something myself:我最终自己实现了一些东西:
class Group<T> {
key:string;
members:T[] = [];
constructor(key:string) {
this.key = key;
}
}
function groupBy<T>(list:T[], func:(x:T)=>string): Group<T>[] {
let res:Group<T>[] = [];
let group:Group<T> = null;
list.forEach((o)=>{
let groupName = func(o);
if (group === null) {
group = new Group<T>(groupName);
}
if (groupName != group.key) {
res.push(group);
group = new Group<T>(groupName);
}
group.members.push(o)
});
if (group != null) {
res.push(group);
}
return res
}
So now I can do所以现在我可以做
const MyComponent = (props:any) => {
return (
<div>
{
groupBy(props.tags, (t)=>t.category_name)
.map((group)=>{
return (
<ul key={group.key}>
<li>{group.key}</li>
<ul>
{
group.members.map((tag)=>{
return <li key={tag.id}>{tag.name}</li>
})
}
</ul>
</ul>
)
})
}
</div>
)
}
Works pretty well, but it is too bad that I need to wrap the list rather than just being able to chain method calls.效果很好,但是我需要包装列表而不仅仅是能够链接方法调用,这太糟糕了。
Is there a better solution?有更好的解决方案吗?
You can use the following code to group stuff using Typescript.您可以使用以下代码使用 Typescript 对内容进行分组。
const groupBy = <T, K extends keyof any>(list: T[], getKey: (item: T) => K) =>
list.reduce((previous, currentItem) => {
const group = getKey(currentItem);
if (!previous[group]) previous[group] = [];
previous[group].push(currentItem);
return previous;
}, {} as Record<K, T[]>);
// A little bit simplified version
const groupBy = <T, K extends keyof any>(arr: T[], key: (i: T) => K) =>
arr.reduce((groups, item) => {
(groups[key(item)] ||= []).push(item);
return groups;
}, {} as Record<K, T[]>);
So, if you have the following structure and array:因此,如果您具有以下结构和数组:
type Person = {
name: string;
age: number;
};
const people: Person[] = [
{
name: "Kevin R",
age: 25,
},
{
name: "Susan S",
age: 18,
},
{
name: "Julia J",
age: 18,
},
{
name: "Sarah C",
age: 25,
},
];
You can invoke it like:您可以像这样调用它:
const results = groupBy(people, i => i.name);
Which in this case, will give you an object with string keys, and Person[] values.在这种情况下,将为您提供一个带有字符串键和 Person[] 值的对象。
There are a few key concepts here:这里有几个关键概念:
1- You can use function to get the key, this way you can use TS infer capabilities to avoid having to type the generic every time you use the function. 1-您可以使用函数来获取密钥,这样您就可以使用 TS 推断功能来避免每次使用函数时都必须键入泛型。
2- By using the K extends keyof any
type constraint, you're telling TS that the key being used needs to be something that can be a key string | number | symbol
2-通过使用
K extends keyof any
,您告诉 TS 正在使用的键需要是可以是键string | number | symbol
string | number | symbol
string | number | symbol
, that way you can use the getKey function to convert Date objects into strings for example. string | number | symbol
,这样您就可以使用 getKey 函数将 Date 对象转换为字符串。
3- Finally, you will be getting an object with keys of the type of the key, and values of the of the array type. 3- 最后,您将获得一个具有键类型的键和数组类型的值的对象。
you could add the function to the array prototype in your app (note some don't recomend this: Why is extending native objects a bad practice? ):您可以将该函数添加到应用程序中的数组原型中(请注意,有些人不建议这样做: 为什么扩展本机对象是一种不好的做法? ):
Array.prototype.groupBy = function(/* params here */) {
let array = this;
let result;
/* do more stuff here*/
return result;
};
Then create an interface in typescript like this:然后在 typescript 中创建一个接口,如下所示:
.d.ts version: .d.ts 版本:
interface Array<T>
{
groupBy<T>(func:(x:T) => string): Group<T>[]
}
OR in a normal ts file:或者在普通的 ts 文件中:
declare global {
interface Array<T>
{
groupBy<T>(func:(x:T) => string): Group<T>[]
}
}
Then you can use:然后你可以使用:
props.tags.groupBy((t)=>t.category_name)
.map((group)=>{
[...]
})
A good option might be lodash
.一个不错的选择可能是
lodash
。
npm install --save lodash
npm install --save-dev @types/lodash
Just import it import * as _ from 'lodash'
and use.只需导入它
import * as _ from 'lodash'
并使用。
Example例子
_.groupBy(..)
_.map(..)
_.filter(..)
During the TC39 meeting of December 2021, the proposal introducing the new Array.prototype.groupBy
and Array.prototype.groupByToMap
function has reached stage 3 in the specification process.在 2021 年 12 月的 TC39 会议期间,引入新的
Array.prototype.groupBy
和Array.prototype.groupByToMap
函数的提案已进入规范流程的第 3 阶段。
Here's how both functions are supposed to look like according to the README linked above:根据上面链接的自述文件,这两个函数应该是这样的:
const array = [1, 2, 3, 4, 5];
// groupBy groups items by arbitrary key.
// In this case, we're grouping by even/odd keys
array.groupBy((num, index, array) => {
return num % 2 === 0 ? 'even': 'odd';
});
// => { odd: [1, 3, 5], even: [2, 4] }
// groupByToMap returns items in a Map, and is useful for grouping using
// an object key.
const odd = { odd: true };
const even = { even: true };
array.groupByToMap((num, index, array) => {
return num % 2 === 0 ? even: odd;
});
// => Map { {odd: true}: [1, 3, 5], {even: true}: [2, 4] }
While not a 100% guaranty that it will really end up in a future version of JavaScript in the form described above (there's always a chance that the proposal can be adjusted or dropped, notably for compatibility reasons), it's nevertheless a strong commitment to have this groupBy
feature offered in the standard lib soon.虽然不能 100% 保证它真的会以上述形式出现在 JavaScript 的未来版本中(提案总是有可能被调整或放弃,特别是出于兼容性原因),但它仍然是一个坚定的承诺这个
groupBy
功能很快就会在标准库中提供。
By ripple effect, it also means that these functions will be also available in TypeScript.通过涟漪效应,这也意味着这些功能也将在 TypeScript 中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.