-1

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..

Aleks_thunder
  • 33
  • 1
  • 5

1 Answers1

0

You have to use item[name]. Otherwise it will try to access property name of item which doesn't exist.

return arrForEachMonth[1]
    .map((item: any) => item[name])
Tobias S.
  • 21,159
  • 4
  • 27
  • 45
  • 2
    This is an **often** repeated duplicate. Please mark duplicates rather than posting answers to them, it's important to the way SO works (basically: not having a thousand versions of the same question with a thousand slightly different answers that are hard to curate). – T.J. Crowder Apr 19 '22 at 17:40
  • @T.J.Crowder Thank you so much, i spent all day at coding and it has last task. I didn't even think about this square brekets. Thanks – Aleks_thunder Apr 19 '22 at 17:43
  • @Aleks_thunder - I think you meant "@​Tobias" :-) – T.J. Crowder Apr 19 '22 at 17:44
  • 1
    @T.J.Crowder oops, ya see i'm done for today :D – Aleks_thunder Apr 19 '22 at 17:46