【问题标题】:Filter and pick values from an array of objects and copy to other array of objects从对象数组中过滤和挑选值并复制到其他对象数组
【发布时间】:2021-09-22 19:03:34
【问题描述】:

我想要实现的是将信息从一个对象数组带到另一个对象数组,找到卡片的最后 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'
        }
    }
];

我想先过滤,然后映射,但效果不佳。

关于如何实现这一点的任何想法?

非常感谢您的帮助!

【问题讨论】:

  • 以下是否回答了您的问题?

标签: arrays filter javascript-objects intersection


【解决方案1】:
  1. 使用Array#reduce,遍历array2,将reissuedCard lastFour存储在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);

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2022-11-30
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多