So I have the following data structures
export class BudgetGroupInfo {
name: string;
bcInfo: BudgetCatInfo[] = [];
}
export class BudgetCatInfo {
name: string;
description: string;
bcAccounts: BCAccountInfo[] = [];
}
export class BCAccountInfo {
name: string;
description: string;
}
What I need to be able to do is find which BudgetGroupInfo
contains both BudgetCatInfo.name
and BCAccountInfo.name
This is what I have so far and it works, but I feel like this isn't the most efficient want to do this.
getGroupInfo(budCat: string, account: string, budgetGroupInfo: BudgetGroupInfo[]): BudgetGroupInfo {
let groupInfo = null;
budgetGroupInfo.forEach(infoGroup => {
infoGroup.bcInfo.filter(bcInfo => bcInfo.name == budCat)
.map(bcInfo => bcInfo.bcAccounts)
.forEach(accounts => {
const acctInfo: BCAccountInfo = accounts.find(acct => acct.name == account);
if(acctInfo) {
groupInfo = infoGroup;
}
});
})
return groupInfo;
}
- First I go through the groups and find which ones have the specific
budCat
- I take the valid
BudgetCatInfo
andmap
thebcAccount
array - I search that array and look for the specified
account
- If I find it then I set it for return.
Just a note that there won't always be a result and there will never be multiple matches.