根据打字稿中的密钥合并2个数组(Angular 13)
我有两个阵列每个都有一个通用键的“ productid”。我想根据公共字段“ Productid”合并两个阵列。
数组1:
{productimageId:1,productid:1,productImage:'asf'} {productimageId:2,productid:2,productname:'acch'} {ProductImageId:3,productid:3,productImage:'bmd'}
array 2:
{productid:1,productCode:'a20292',uom:'no',数量:23} {productid:2,productcode:'b8282222',uom:'no',数量:20} {productid:3,productcode:'r29299922',uom:'no',数量:98}
我尝试使用映射,但是我遇到了一个错误“映射不是函数” “ core.mjs:6494错误typeerror:array1.map不是一个函数
,这里是我尝试过的代码,
`mergeById(array1:any, array2:any) {
array1.map((itm:any) => ({
...array2.find((item:any) => (item.productID === itm.productID) && item),
...itm
}));
}`
我在这里粘贴了,我想根据匹配的“ productid”合并这两个阵列。任何帮助都非常感谢。
I am having 2 arrays each having a common key "productID". I want to merge the 2 arrays based on the common field "productID".
array 1:
{productImageID: 1, productID: 1, productImage: 'asf'}
{productImageID: 2, productID: 2, productName:'anx'}
{productImageID: 3, productID: 3, productImage: 'bmd'}array 2:
{productID: 1, productCode: 'A20292', uom: 'No', quantity: 23}
{productID: 2, productCode: 'B8282222', uom: 'No', quantity: 20}
{productID: 3, productCode: 'R29299922', uom: 'No', quantity: 98}
I tried using map, but I am getting an error "map is not a function"
"core.mjs:6494 ERROR TypeError: array1.map is not a function
Here is the code that I tried
`mergeById(array1:any, array2:any) {
array1.map((itm:any) => ({
...array2.find((item:any) => (item.productID === itm.productID) && item),
...itm
}));
}`
I am stuck here and I want to merge both these arrays based on the matching "productID". Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您使用该功能正在做错事。
我定义了该功能,
请参见上面的使用正确类型的含义。否则,您的IDE无法在编译时间告诉您该类型是否具有MAP功能。
我使用了以下方式:
结果似乎是正确的。
我在您的功能中看到的是您不返回某物。也许这是问题。
I guess you are doing something wrong, using the function.
I defined the function
See above what a mean with using correct types. Otherwise your IDE is unable to tell you on compile time if the type has map function or not.
And i used it the following way:
The result seems to be correct.
What i see in you function is that you do not return something. Maybe this is the issue.