打字稿中的Map 2数组对象

Map 2 array object in typescript

我有 2 个数组对象,link 按 CustomerID。结果数组将从 Customers 添加到 Licenses。

许可证:

[
  {
    _id: new ObjectId("625642b73dc060ffc4c16b3e"),
    CustomerID: '61c140a22465edbdc9cdb041',
    Note: 'Test',
    Active: true,
    CreatedByUserID: '61c015e926f22813e9e4c3c5',
    UpdatedByUserID: '61c015e926f22813e9e4c3c5',
    createdAt: 2022-04-13T03:25:43.833Z,
    updatedAt: 2022-04-13T03:25:43.833Z,
    __v: 0
  },
  {
    _id: new ObjectId("624d05b7c0de23c18c987898"),
    CustomerID: '61bc280b3a67511ad62aefc7',
    Note: 'Ideapad Testing',
    Active: true,
    CreatedByUserID: '',
    UpdatedByUserID: '',
    createdAt: 2022-04-06T03:15:03.669Z,
    updatedAt: 2022-04-06T03:15:03.669Z,
    __v: 0
  }
]

客户:

  [
    { Name: 'Test1', id: '61bc280b3a67511ad62aefc7' },
    { Name: 'Test2', id: '61c140a22465edbdc9cdb041' }
  ]

我想将 CustomerName 键值添加到来自客户的许可证,并具有如下结果数组对象:

[
      {
        _id: new ObjectId("625642b73dc060ffc4c16b3e"),
        CustomerID: '61c140a22465edbdc9cdb041',
        CustomerName: 'Test2',
        Note: 'Test',
        Active: true,
        CreatedByUserID: '61c015e926f22813e9e4c3c5',
        UpdatedByUserID: '61c015e926f22813e9e4c3c5',
        createdAt: 2022-04-13T03:25:43.833Z,
        updatedAt: 2022-04-13T03:25:43.833Z,
        __v: 0
      },
      {
        _id: new ObjectId("624d05b7c0de23c18c987898"),
        CustomerID: '61bc280b3a67511ad62aefc7',
        CustomerName: 'Test1',
        Note: 'Ideapad Testing',
        Active: true,
        CreatedByUserID: '',
        UpdatedByUserID: '',
        createdAt: 2022-04-06T03:15:03.669Z,
        updatedAt: 2022-04-06T03:15:03.669Z,
        __v: 0
      }
    ]

我试过这段代码,但没有成功:

Licenses.reduce((acc: Array<object>, curr) => {
              const stored = Customers.find(
                ({ id }) => id === curr.CustomerID
              );

              if (stored) {
                console.log('stored.Name', stored.Name);
                curr['CustomerName'] = stored.Name;
                acc.push(stored);
              }
              return acc;
            }, []);

你可以这样做:

  • array.forEach 循环迭代每个许可证

  • array.find找客户link到customerID

    licences.forEach(licence => {
      var found = customers.find(cust => cust.id === licence.CustomerID);
      if (found) {
        licence['CustomerName'] = found.Name;
      }
    });
    

let licences = [{
    _id: "625642b73dc060ffc4c16b3e",
    CustomerID: '61c140a22465edbdc9cdb041',
    Note: 'Test',
    Active: true,
    CreatedByUserID: '61c015e926f22813e9e4c3c5',
    UpdatedByUserID: '61c015e926f22813e9e4c3c5',
    createdAt: '2022 - 04 - 13 T03: 25: 43.833 Z',
    updatedAt: '2022 - 04 - 13 T03: 25: 43.833 Z',
    __v: 0
  },
  {
    _id: "624d05b7c0de23c18c987898",
    CustomerID: '61bc280b3a67511ad62aefc7',
    Note: 'Ideapad Testing',
    Active: true,
    CreatedByUserID: '',
    UpdatedByUserID: '',
    createdAt: '2022 - 04 - 06 T03: 15: 03.669 Z',
    updatedAt: '2022 - 04 - 06 T03: 15: 03.669 Z',
    __v: 0
  }
];

const customers = [{
    Name: 'Test1',
    id: '61bc280b3a67511ad62aefc7'
  },
  {
    Name: 'Test2',
    id: '61c140a22465edbdc9cdb041'
  }
];

licences.forEach(licence => {
  const found = customers.find(cust => cust.id === licence.CustomerID);
  if (found) {
    licence['CustomerName'] = found.Name;
  }
});;

console.log(licences);

// create customer map for better perfomance:
const customerMap = new Map(customers.map(customer => ([customer.id, customer])));

// map Licenses and populate required data:
const populatedLicenses = licenses.map(license => {
  const customer = customerMap.get(license.CustomerID);

  return ({
    ...license,
    CustomerName: customer.Name,
  });
});