开发者问题收集

如何在 Javascript 中将一个数组对象中的属性值替换为另一个数组对象中的属性值

2022-08-18
572

我有一个数组对象

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

我想用 admins 的名称替换成员 arrayObject 的 AddedByAdminId 的值,其中 admins.id = AddedByAdminId

我当前的代码:

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

预期结果:

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

我得到的错误: 未捕获的 TypeError:无法读取未定义的属性(读取“name”)

我正在使用 React。

3个回答

这是您要执行的操作:

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}
});

结果

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

但这是我的建议: 不要用与此变量名不匹配的值覆盖变量,而是创建一个新变量。

代码:

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

结果

[
    {
        "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"
    }
]
moolsbytheway
2022-08-18

所以像这样?

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)
Reinier68
2022-08-18

当管理数组中存在匹配的记录时,这将用名称替换addedByAdminId。如果没有找到与addedByAdminId匹配的记录,则不会替换addedByAdminId。

{
    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);
Nipuna Jayawardana
2022-08-18