Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

function argument as map value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading