Good day, i have an Array of objects, and im trying to map entries.
const monthOnject = {
"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've got them by passing straight methods like:
this.monthlyCategories = Object.entries(this.monthlyObject)
.map((arrForEachMonth: Array<any>) => {
return arrForEachMonth[1]
.map((item: any) => item.category);
});
And it was working, but i dont like to repeate code cuz i will call "amount, take, category", so i'm trying to reduce code by to passing an function argument as a map value (item.name). And result is undefined. Tell me please what is my misstake
const monthObjEntries = (name: string) => Object
.entries(this.monthlyObject)
.map((arrForEachMonth: Array<any>) => {
return arrForEachMonth[1]
.map((item: any) => item.name) // <- problem is here gives undefined
})
console.log(monthObjEntries('amount'));
Actually i understood that argument name is not going to item.name. But how to manage this i don't know..