Hello there, I have an array of objects that I am attempting to map through.
const monthObject = {
"March 2022": [
{
"date": "2022-03-16",
"amount": "-50",
"description": "mar ",
"category": "Salary",
"createdAt": "2022-04-15T06:27:16.953Z",
"updatedAt": "2022-04-15T06:27:16.953Z",
"__v": 0
}
],
"April 2022": [
{
"date": "2022-04-15",
"amount": "100",
"description": "apr",
"category": "Debt",
"createdAt": "2022-04-15T06:27:02.381Z",
"updatedAt": "2022-04-15T06:27:02.381Z",
"__v": 0
},
{
"date": "2022-04-19",
"amount": "-150",
"description": "apr",
"category": "Salary",
"createdAt": "2022-04-15T06:27:33.035Z",
"updatedAt": "2022-04-16T11:48:39.540Z",
"__v": 0
},
{
"date": "2022-04-26",
"amount": "11",
"description": "asd",
"category": "Credit",
"createdAt": "2022-04-15T18:03:24.785Z",
"updatedAt": "2022-04-16T11:51:49.503Z",
"__v": 0
},
{
"date": "2022-04-27",
"amount": "111",
"description": "asdad",
"category": "Debt",
"createdAt": "2022-04-19T13:59:30.821Z"
"updatedAt": "2022-04-19T13:59:30.821Z",
"__v": 0
}
],
"May 2022": [
{
"date": "2022-05-18",
"amount": "-200",
"description": "may",
"category": "Salary",
"createdAt": "2022-04-15T06:27:42.184Z",
"updatedAt": "2022-04-15T06:27:42.184Z",
"__v": 0
}
],
"June 2022": [
{
"date": "2022-06-22",
"amount": "290",
"description": "aaa",
"category": "Investments",
"createdAt": "2022-04-16T12:29:27.857Z",
"updatedAt": "2022-04-16T12:29:27.857Z",
"__v": 0
}
]
}
I initially accessed them using direct methods like:
this.monthlyCategories = Object.entries(this.monthlyObject)
.map((arrForEachMonth: Array<any>) => {
return arrForEachMonth[1]
.map((item: any) => item.category);
});
This method was functional, but I wanted to avoid code repetition by passing a function argument as the mapped value (item.name). However, the result turned out to be undefined. Could you please point out what error I might have made?
const monthObjEntries = (name: string) => Object
.entries(this.monthlyObject)
.map((arrForEachMonth: Array<any>) => {
return arrForEachMonth[1]
.map((item: any) => item.name) // <- the problem lies here and returns undefined
})
console.log(monthObjEntries('amount'));
After reflection, I realized that the argument name is not being passed correctly to item.name. I'm unsure about how to resolve this issue...