从对象数组中筛选和选取值,然后复制到其他对象数组

Filter and pick values from an array of objects and copy to other array of objects

我想要实现的是将信息从一个对象数组带到另一个对象数组,找到一张卡片的最后 4 个数字的匹配项,从该数组中提取具有值的键并将它们带入到另一个对象数组。

const array1 = [
    {
        card: {
            expiryDate: '2025-06-30',
            isPinSet: true,
            issuedDate: '2021-06-02',
            lastNumbers: '2377',
            status: 'ACTIVE',
            type: 'PHYSICAL'
        }
    },
    {
        card: {
            expiryDate: '2024-11-30',
            isPinSet: true,
            issuedDate: '2020-12-14',
            lastNumbers: '7231',
            status: 'TERMINATED',
            type: 'PHYSICAL',
            terminationDate: '2021-06-02'
        }
    }
];

const array2 = [
    {
        dateCreated: '2021-06-02T11:14:27.348Z[UTC]',
        link: 'juan ',
        reason: 'lost card',
        reissueRequest: {
            link: 'juan ',
            reason: 'lost card',
            cardType: 'PHYSICAL',
            personId: '2576890',
            adminUser: 'juan@hotmail.com',
            changePAN: true,
            lastNumbers: '7231',
            maintainPIN: false
        },
        reissuedCard: {
            state: 'UNACTIVATED',
            cardId: null,
            cardType: 'PHYSICAL',
            isPinSet: true,
            lastFour: '2377',
            dateCreated: '2021-06-02',
            expirationDate: '2025-06-30'
        }
    }
];

我期待这样的结果:

const result = [
    {
        card: {
            expiryDate: '2025-06-30',
            isPinSet: true,
            issuedDate: '2021-06-02',
            lastNumbers: '2377',
            status: 'ACTIVE',
            type: 'PHYSICAL',
            reissuedCard: {
                state: 'UNACTIVATED',
                cardId: null,
                cardType: 'PHYSICAL',
                isPinSet: true,
                lastFour: '2377',
                dateCreated: '2021-06-02',
                expirationDate: '2025-06-30'
            }
        }
    },
    {
        card: {
            expiryDate: '2024-11-30',
            isPinSet: true,
            issuedDate: '2020-12-14',
            lastNumbers: '7231',
            status: 'TERMINATED',
            type: 'PHYSICAL',
            terminationDate: '2021-06-02'
        }
    }
];

我想先过滤,然后再映射,但效果不是很好。

关于如何实现这一点有什么想法吗?

非常感谢您的帮助!

  1. 使用Array#reduce, iterate over array2 to store the reissuedCard and the lastFour in a Map
  2. 使用 Array#map,遍历 array1 并与元素合并其在地图中的匹配项(如果存在)

const 
  array1 = [
    { card: { expiryDate: '2025-06-30', isPinSet: true, issuedDate: '2021-06-02', lastNumbers: '2377', status: 'ACTIVE', type: 'PHYSICAL' } },
    { card: { expiryDate: '2024-11-30', isPinSet: true, issuedDate: '2020-12-14', lastNumbers: '7231', status: 'TERMINATED', type: 'PHYSICAL', terminationDate: '2021-06-02' } }
  ],
  array2 = [
    {
      dateCreated: '2021-06-02T11:14:27.348Z[UTC]',
      link: 'juan ',
      reason: 'lost card',
      reissueRequest: { link: 'juan ', reason: 'lost card', cardType: 'PHYSICAL', personId: '2576890', adminUser: 'juan@hotmail.com', changePAN: true, lastNumbers: '7231', maintainPIN: false },
      reissuedCard: { state: 'UNACTIVATED', cardId: null, cardType: 'PHYSICAL', isPinSet: true, lastFour: '2377', dateCreated: '2021-06-02', expirationDate: '2025-06-30' }
    }
  ];

const reissuedCardMap = array2.reduce((map, { reissuedCard }) => 
  map.set(reissuedCard.lastFour, { reissuedCard })
, new Map);

const res = array1.map(({ card }) => ({
  card: { ...card, ...(reissuedCardMap.get(card.lastNumbers) || {}) }
}));

console.log(res);