I have an array of names for an account. Each person can have more than one type. type is an enum..which should replace the values to their definitions. Want to group by accountNumber, firstName, lastName and not type.
export interface Name {
accountNumber: string;
firstName: string|null;
lastName: string|null;
type: AccountType;
}
export enum AccountType{
primary = 0,
secondary = 1
}
const names =[
{
"accountNumber":"0000001420",
"firstName":"KELLY",
"lastName":"WHITE",
"type":0
},
{
"accountNumber":"0000001420",
"firstName":"RAY",
"lastName":"WHITE",
"type":0
},
{
"accountNumber":"0000001420",
"firstName":"KELLY",
"lastName":"WHITE",
"type":1
}
]
I want an output array with the following format:
[{
firstName:"KELLY",
lastName:"WHITE",
accountNumber:"0000001420",
types: [primary, secondary]
}, {
firstName: "RAY",
lastName: "WHITE",
accountNumber: "0000001420",
types: [primary]
}]
I tried using Reduce...
const merged2 = names.reduce((r, { accountNumber, firstName, lastName,type }) => {
const key = `${accountNumber}-${firstName}-${lastName}`;
r[key] = r[key] || { accountNumber, firstName, lastName, types: [] };
r[key]["types"].push(type)
return r;
}, {})
I get error at r[key]
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'.
How to fix the error or is there any other way to get the result instead of Reduce function?