Home > database >  How to Replace values of a property in an Array Object with values of a property in another Array Ob
How to Replace values of a property in an Array Object with values of a property in another Array Ob

Time:08-18

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

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.

CodePudding user response:

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"
    }
]

CodePudding user response:

So like this?

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

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

const objC = members.map(({ id, name, addedByAdminId }) => ({
  id,
  name,
  addedByAdminId: admins[addedByAdminId - 1].name
}))

console.log(objC)

CodePudding user response:

this will replace addedByAdminId with the name when a matching record is present in admin array. if there is no matching record found to the addedByAdminId, the addedByAdminId will be not replaced.

  {
    id: 1,
    name: 'Admin 1',
  },
  {
    id: 2,
    name: 'Admin 2',
  },
  {
    id: 3,
    name: 'Admin 3',
  }
]

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

const result = members.map(({ id, name, addedByAdminId }) => ({
  id,
  name,
  addedByAdminId: admins.some(admin => admin.id === addedByAdminId) ? admins.find(admin => admin.id === addedByAdminId).name : addedByAdminId
}))

console.log(result);
  • Related