我正在尝试将平面的账户余额列表转换为基于唯一代码列表的分层账户树列表。我不知道该如何解决这个问题,因为我不知道该采取什么步骤。
如果您考虑 Excel 表,那么帐户余额将是一行,而代码字段表示该行被分解为多少个级别的列。这实际上意味着第一个代码 [0] 是第一级,代码 [1] 将是下一个,依此类推。通过将帐户余额转换为帐户树,我希望正确地对每个级别(代码类型和代码)的 amountOut 求和,然后将其作为帐户树添加到 subAccounts 数组中。
下面我添加了所有使用的类型并提供了输入和输出的示例。
interface AccountBalance {
id: string
codes: Code[]
amountOut: number
}
type Code = {
id: string
codeType: CodeTypes
code: string
description: string
}
enum CodeTypes {
account = "account",
responsible = "responsible",
project = "project",
object = "object",
counterPart = "counterPart",
free = "free",
}
type AccountTree = {
id: string
name: string
code: string
amountOut: number
subAccounts: AccountTree[]
}
输入示例:
const accountBalances: AccountBalance[] = [
{
id: "671769fbd36fcd6c2c7f2d9b",
codes: [
{
id: "671769fbd36fcd6c2c7f2c2d",
codeType: codeTypeEnum.account,
code: "1250",
description: "Column A",
},
{
id: "671769fbd36fcd6c2c7f2bd5",
codeType: codeTypeEnum.responsible,
code: "17",
description: "Column B",
},
{
id: "671769fbd36fcd6c2c7f2bf7",
codeType: codeTypeEnum.counterPart,
code: "20",
description: "Column C",
},
],
amountOut: 24510549,
},
{
id: "671769fbd36fcd6c2c7f2d9c",
codes: [
{
id: "671769fbd36fcd6c2c7f2c2d",
codeType: codeTypeEnum.account,
code: "1250",
description: "Column A",
},
{
id: "671769fbd36fcd6c2c7f2bee",
codeType: codeTypeEnum.responsible,
code: "40",
description: "Column B",
},
{
id: "671769fbd36fcd6c2c7f2c08",
codeType: codeTypeEnum.counterPart,
code: "S3",
description: "Column C",
},
],
amountOut: 0,
},
{
id: "671769fbd36fcd6c2c7f2d9d",
codes: [
{
id: "671769fbd36fcd6c2c7f2c2d",
codeType: codeTypeEnum.account,
code: "1250",
description: "Column A",
},
{
id: "671769fbd36fcd6c2c7f2bdb",
codeType: codeTypeEnum.responsible,
code: "80",
description: "Column B",
},
{
id: "671769fbd36fcd6c2c7f2bdc",
codeType: codeTypeEnum.counterPart,
code: "52",
description: "Column C",
},
],
amountOut: 6381398,
},
]
我想要的输出,例如:
const expected: AccountTree = {
id: "671769fbd36fcd6c2c7f2c2d",
name: "Column A",
code: "1250",
amountOut: 30891947,
subAccounts: [
{
id: "671769fbd36fcd6c2c7f2bd5",
name: "Column B",
code: "17",
amountOut: 24510549,
subAccounts: [
{
id: "671769fbd36fcd6c2c7f2bf7",
name: "Column C",
code: "20",
amountOut: 24510549,
subAccounts: [],
},
],
},
{
id: "671769fbd36fcd6c2c7f2bee",
name: "Column B",
code: "40",
amountOut: 0,
subAccounts: [
{
id: "671769fbd36fcd6c2c7f2c08",
name: "Column C",
code: "S3",
amountOut: 0,
subAccounts: [],
},
],
},
{
id: "671769fbd36fcd6c2c7f2bdb",
name: "Column B",
code: "80",
amountOut: 6381398,
subAccounts: [
{
id: "671769fbd36fcd6c2c7f2bdc",
name: "Column C",
code: "52",
amountOut: 6381398,
subAccounts: [],
},
],
},
],
}
到目前为止我得到的是
const uniqueCodeTypes = [
...new Set(
accountBalances.reduce<CodeTypes[]>(
(acc, balance) => [
...acc,
...balance.codes.flatMap((code) => code.codeType),
],
[],
),
),
]
const uniqueCodesForCodeType = uniqueCodeTypes.map((codeType) => {
const uniqueCodesForCodeType = [
...new Map(
accountBalances.reduce<Code[]>((acc, balance) => {
const code = balance.codes.find(
(code) => code.codeType === codeType,
)
return code ? [...acc, code] : acc
}, []).map((code) => [code.code, code])
).values(),
]
return uniqueCodesForCodeType
})
但我不知道此后该如何继续。