根据打字稿中的密钥合并2个数组(Angular 13)

发布于 2025-01-31 11:47:05 字数 801 浏览 2 评论 0原文

我有两个阵列每个都有一个通用键的“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2025-02-07 11:47:05

我想您使用该功能正在做错事。

我定义了该功能,

function mergeById(array1: { productID: number }[], array2: { productID: number}[]): { productID: number }[] {
  return array1.map((itm) => ({
    ...array2.find((item) => (item.productID === itm.productID)),
    ...itm,
   }));
}

请参见上面的使用正确类型的含义。否则,您的IDE无法在编译时间告诉您该类型是否具有MAP功能。

我使用了以下方式:

const array1 = [
    { productImageID: 1, productID: 1, productImage: 'asf' },
    { productImageID: 2, productID: 2, productName:'anx' },
    { productImageID: 3, productID: 3, productImage: 'bmd' },
];
const array2 = [
    { productID: 1, productCode: 'A20292', uom: 'No', quantity: 23 },
    { productID: 2, productCode: 'B8282222', uom: 'No', quantity: 20},
    { productID: 3, productCode: 'R29299922', uom: 'No', quantity: 98 }
];
console.log(mergeById(array1, array2));

结果似乎是正确的。

我在您的功能中看到的是您不返回某物。也许这是问题。

I guess you are doing something wrong, using the function.

I defined the function

function mergeById(array1: { productID: number }[], array2: { productID: number}[]): { productID: number }[] {
  return array1.map((itm) => ({
    ...array2.find((item) => (item.productID === itm.productID)),
    ...itm,
   }));
}

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:

const array1 = [
    { productImageID: 1, productID: 1, productImage: 'asf' },
    { productImageID: 2, productID: 2, productName:'anx' },
    { productImageID: 3, productID: 3, productImage: 'bmd' },
];
const array2 = [
    { productID: 1, productCode: 'A20292', uom: 'No', quantity: 23 },
    { productID: 2, productCode: 'B8282222', uom: 'No', quantity: 20},
    { productID: 3, productCode: 'R29299922', uom: 'No', quantity: 98 }
];
console.log(mergeById(array1, array2));

The result seems to be correct.

What i see in you function is that you do not return something. Maybe this is the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文