Im being asked to create an app that has a user list which also has expenses linked to the user.
I decided on creating 2 hash tables/dictionaries to handle this, but is there a better way to do this?
I’m thinking maybe theres a better way like using a tree since when I delete an expense from the expenseList, I cannot seem to delete the corresponding expenseId from the userList without iterating through it entirely. Im trying to find a way where I can delete the expense from the userList and expenseList the most optimal way.
All expenses must also be linked to a user.
Below is the initial datastructure I decided to go with:
export const userList = {
1: {
userId:1,
firstname:'Jonathan',
lastname: 'Lee',
expenseIds: [
10,
20,
30,
40
]
},
2: {
userId:2,
firstname:'Todd',
lastname: 'Don',
expenseIds: [
50
]
}
};
export const expenseList = {
10:{
userId: 1,
expId: 10,
fullname: 'Jonathan Lee',
category: 'food',
description: 'had dinner with client',
cost: 130.31
},
20:{
userId: 1,
expId: 20,
fullname:'Jonathan Lee',
category: 'travel' ,
description: 'had dinner with client',
cost: 50.00
},
30:{
userId: 1,
expId: 30,
fullname:'Jonathan Lee',
category: 'travel' ,
description: 'travel expenses',
cost: 150.00
},
40:{
userId: 1,
expId: 40,
fullname:'Jonathan Lee',
category: 'equipment' ,
description: 'expensing new laptop',
cost: 1500.00
},
50:{
userId: 2,
expId: 50,
fullname:'Todd Don',
category: 'equipment' ,
description: 'expensing new laptop',
cost: 1100.00
} };