我想使用两个数组创建一个新数组。
itemsAll = [
{id: 1, itemId: 1, serialNo:"11111111", itemTypeId: 1, itemMasterId: 1, itemStatus: 0, updatedBy: 1 },
{id: 2, itemId: 2, serialNo:"2222222", itemTypeId: 3, itemMasterId: 2, itemStatus: 0, updatedBy: 1 }
];
我想获取与itemTypeId匹配的itemTypeName。
itemType = [
{id: 3, itemTypeId: 1, itemTypeName: "type Name 1", description: "Hello", itemTypeStatus: 0, status: true },
{id: 13, itemTypeId: 2, itemTypeName: "type Name 2", description: "222 Hello", itemTypeStatus: 0, status: true },
{id: 15 , itemTypeId: 3, itemTypeName: "type Name 3", description: "333 Hello", itemTypeStatus: 0, status: true }
];
期望数组
itemsAllNew = [
{id: 1, itemId: 1, serialNo:"11111111", itemTypeId: 1, itemTypeName: "type Name 1", itemMasterId: 1, itemStatus: 0, updatedBy: 1 },
{id: 2, itemId: 2, serialNo:"2222222", itemTypeId: 3, , itemTypeName: "type Name 3", itemMasterId: 2, itemStatus: 0, updatedBy: 1 }
];
我尝试过以下解决方案,但它包含不必要的键值对。
const output = itemsAll.map(
itemsall => Object.assign(itemsall, itemType.find((itemtype) => itemtype.itemTypeId === itemsall.itemTypeId))
);
console.log(output);
输出结果的截图如下。