Home > Mobile >  Map 2 array object in typescript
Map 2 array object in typescript

Time:04-13

I have 2 array object, link by CustomerID. The result array will be add CustomerName from Customers to Licenses.

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

Customers:

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

I want add a CustomerName key value to Licenses from Customers and have result array object as below:

[
      {
        _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
      }
    ]

I tried with this code, but not success:

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;
            }, []);

CodePudding user response:

you can do it with :

  • array.forEach loop to iterate on each licence

  • array.find to find the customer link to the 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);

CodePudding user response:

// 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,
  });
});
  • Related