使用两个数组的特定数据和值创建新数组

3

我想使用两个数组创建一个新数组。

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

输出结果的截图如下。

输入图片说明


请添加您尝试过的代码。 - adiga
请访问[帮助],参观[tour]以了解并[提问]。进行一些研究,在SO上搜索相关主题;如果遇到困难,请发布您的尝试的[mcve],注明输入和预期输出,最好在[Stacksnippet]中。 - mplungjan
foreach 和 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/find - mplungjan
你真正想要什么并不清楚。你没有给我们关联这两个矩阵的标准,也没有给我们尝试的机会。 - Aks Jacoves
1
@adiga,我加了我尝试过的代码。 - Pramith Chathuranga
@AksJacoves 我添加了我尝试过的内容,请检查。 - Pramith Chathuranga
3个回答

2
你可以创建一个 Map 对象,并使用时间复杂度 O(1) 进行映射:
const mapItemType = new Map(itemType.map(i => [i.itemTypeId, i.itemTypeName]));
const result = itemsAll.map(({itemTypeId, ...other}) => 
   ({itemTypeName: mapItemType.get(itemTypeId), ...other }))

一个例子:

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

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

const mapItemType = new Map(itemType.map(i => [i.itemTypeId, i.itemTypeName]));
const result = itemsAll.map(({itemTypeId, ...other}) => ({itemTypeName: mapItemType.get(itemTypeId), ...other }))
console.log(result)


1
你可以循环遍历itemsAll数组并检查其中的项目是否属于itemType数组中的itemTypeId

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

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

const findItemTypes = (items, types) => {
  return items.map(item => {
    const { itemTypeName } = (types.find(type => type.itemTypeId === item.itemTypeId) || {});
    return {
    ...item,
    itemTypeName
    }
  })
}

console.log(findItemTypes(itemsAll, itemType))
.as-console-wrapper {
  max-height: 100% !important;
}


1
有一个不需要的数据 "description": "Hello" 如何排除它们。 - Pramith Chathuranga

1

你的意思是你得到了额外的键值对。例如:description: '222 Hello',但你不需要它。如果你的解决方案正常工作,那么你所需要做的就是删除不必要的字段。一种方法是使用map函数。

const newArr = arr.map(({Title, ...rest}) => ({...rest})) 

这将删除标题字段并提供其余字段。您可以根据您的用例类似地操作。

2
可以简化为 arr.map(({ Title, ...rest}) => rest) - adiga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接