我需要编写一个groupBy函数,使用给定的函数对JSON数据中的人员进行分组。下面是接口和JSON数据:
interface Person {
name: string;
yearOfBirth: number;
placeOfBirth: string;
}
const input: Person[] = [
{
name: "Andy",
yearOfBirth: 1984,
placeOfBirth: "New York",
},
{
name: "John",
yearOfBirth: 1995,
placeOfBirth: "New York",
},
{
name: "Bill",
yearOfBirth: 1995,
placeOfBirth: "Orlando",
},
{
name: "Tim",
yearOfBirth: 1989,
placeOfBirth: "Witchita",
},
];
下面是我到目前为止的代码,它还没有接近工作,因为它应该,但我是新的回调函数,所以我一直试图得到它是如何工作的感觉。
const groupBy = (input: Person[], func: (p: Person) => string): string[] => {
let collect: string[] = [];
for (let i = 0; i < input.length; i++) {
collect[i] = func(input[i]);
}
return collect;
}
try {
console.log(groupBy(input, (p: Person) => p.placeOfBirth));
}
catch (e: any) {
console.log(e.message);
}
//This is the output from my current code:
[ 'New York', 'New York', 'Orlando', 'Witchita' ]
下面是使用下面的console.log调用时的实际输出:
//Example 1
//call the function which groups the data together from 'placeOfBirth':
console.log(groupBy(input, (p: Person) => p.placeOfBirth));
//Output from console.log:
{
New York: [
{ name: 'Andy', yearOfBirth: 1984, placeOfBirth: 'New York' },
{ name: 'John', yearOfBirth: 1995, placeOfBirth: 'New York' }
],
Orlando: [ { name: 'Bill', yearOfBirth: 1995, placeOfBirth: 'Orlando' } ],
Witchita: [ { name: 'Tim', yearOfBirth: 1989, placeOfBirth: 'Witchita' } ]
}
//Example 2
//call the function which groups the data together from 'yearOfBirth':
console.log(groupBy(input, (p: Person) => p.yearOfBirth));
//Output from console.log:
{
'1984': [ { name: 'Andy', yearOfBirth: 1984, placeOfBirth: 'New York' } ],
'1995': [
{ name: 'John', yearOfBirth: 1995, placeOfBirth: 'New York' },
{ name: 'Bill', yearOfBirth: 1995, placeOfBirth: 'Orlando' }
],
'1989': [ { name: 'Tim', yearOfBirth: 1989, placeOfBirth: 'Witchita' } ]
}
3条答案
按热度按时间dojqjjoe1#
您是否注重性能?
如果没有,您可以尝试以下操作:
这里有一个沙箱来尝试一下:https://codesandbox.io/s/unruffled-morse-nwjjsy?file=/src/index.ts
e37o9pze2#
使用下面的函数,传递你的对象数组和键名进行分组,然后根据你发送的键获取分组数据:
nxowjjhe3#
我找到了一个解决方案,最终将数据分组在一起。我将分享下面的代码给其他寻找类似解决方案的人: