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

How to Replace values of a property in an Array Object with values of a property in another Array Object in Javascript

I have an Array Object

    const admins= [
    {
        id: 1,
        name: 'Admin 1',
    },
    {
        id: 2,
        name: 'Admin 2',
    },
    {
        id: 3,
        name: 'Admin 3',
    }
]

and another Array Object

const members= [
    {
        id: 1,
        name: 'Name 1',
        addedByAdminId: 1
    },
    {
        id: 2,
        name: 'Name 2',
        addedByAdminId: 2
    },
    {
        id: 3,
        name: 'Name 3',
        addedByAdminId: 3
    }
]

I want to replace values of addedByAdminId of member arrayObject by names of admins where admins.id = addedByAdminId

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

My Current Code :

const objectC = members.forEach((item) => item.addedByAdminId= admins.filter(obj => obj.id === item.addedByAdminId)[0]['name']);

Expected Result :

objectC = [
    {
        id: 1,
        name: 'Name 1',
        addedByAdminId: 'Admin 1'
    },
    {
        id: 2,
        name: 'Name 2',
        addedByAdminId: 'Admin 2'
    },
    {
        id: 3,
        name: 'Name 3',
        addedByAdminId: 'Admin 3'
    }
]

Error I am Getting :
Uncaught TypeError: Cannot read properties of undefined (reading ‘name’)

I am using React.

>Solution :

Welcome to stackoverflow.

This is what you want to do:

const newMembers = members.map(member => {
    // get the admin name
    const adminName = admins.find(it => it.id === member.addedByAdminId)?.name;
    // create a new object with the spread operator
    // containing everything from the memmber object 
    // overriding the property addedByAdminId with the variable adminName
    return {...member, addedByAdminId: adminName}
});

Result

[
    {
        "id": 1,
        "name": "Name 1",
        "addedByAdminId": "Admin 1"
    },
    {
        "id": 2,
        "name": "Name 2",
        "addedByAdminId": "Admin 2"
    },
    {
        "id": 3,
        "name": "Name 3",
        "addedByAdminId": "Admin 3"
    }
]

But this is what i suggest:
Instead of overriding a variable with a value that doesn’t match this variable name just create a new one.

Code:

const newMembers = members.map(member => {
    const adminName = admins.find(it => it.id === member.addedByAdminId)?.name;
    return {...member, addedByAdminName: adminName}
});

Result

[
    {
        "id": 1,
        "name": "Name 1",
        "addedByAdminId": 1,
        "addedByAdminName": "Admin 1"
    },
    {
        "id": 2,
        "name": "Name 2",
        "addedByAdminId": 2,
        "addedByAdminName": "Admin 2"
    },
    {
        "id": 3,
        "name": "Name 3",
        "addedByAdminId": 3,
        "addedByAdminName": "Admin 3"
    }
]
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