【问题标题】:filter array of objects based on another array of objects and return merged array根据另一个对象数组过滤对象数组并返回合并数组
【发布时间】:2018-08-22 09:56:28
【问题描述】:

我有一个这样的对象数组:

phoneContacts= [
{
  firstName: "aaaa", 
  lasttName: "aaaa", 
  phoneNumbers: [{id: "1", label: "mobile", number: "09121111111"},{id: "1", label: "home", number: "02188888888"}]
},
{
  firstName: "bbbb", 
  lasttName: "bbbb", 
  phoneNumbers: [{id: "1", label: "mobile", number: "09122222222"},{id: "1", label: "home", number: "02177777777"}],
},
...]

我想用这样的数组过滤它:

registeredUsers= [
{
  ID: 1, 
  CellPhone: "09123333333"
},
{
  ID: 2, 
  CellPhone: "09121111111"
},
...]

并返回:

contactsMergerdWithID= [
{
  ID: 1,
  firstName: "aaaa", 
  lasttName: "aaaa", 
  phoneNumbers: [{id: "1", label: "mobile", number: "09121111111"},{id: "1", label: "home", number: "02188888888"}]
},
{
  ID: 0,            // or without ID
  firstName: "bbbb", 
  lasttName: "bbbb", 
  phoneNumbers: [{id: "1", label: "mobile", number: "09122222222"},{id: "1", label: "home", number: "02177777777"}]
},
...]

如果其中任何一个 mobilephoneNumbers 与第二个数组中的 CellPhone 匹配,我想在第二个数组中返回具有匹配 ID 字段的第一个数组。 我该怎么做?

【问题讨论】:

    标签: javascript arrays object filter


    【解决方案1】:

    const registeredUsers= [ {
      ID: 1, 
      CellPhone: "09123333333"
    }, {
      ID: 2, 
      CellPhone: "09121111111"
    }];
    
    phoneContacts= [ {
      firstName: "aaaa", 
      lasttName: "aaaa", 
      phoneNumbers: [{id: "1", label: "mobile", number: "09121111111"},{id: "1", label: "home", number: "02188888888"}]
    }, {
      firstName: "bbbb", 
      lasttName: "bbbb", 
      phoneNumbers: [{id: "1", label: "mobile", number: "09122222222"},{id: "1", label: "home", number: "02177777777"}],
    }]
    
    const contactsMergerdWithID = phoneContacts.map(contact => {
      const mobile = contact.phoneNumbers.find(phoneNumber => phoneNumber.label === 'mobile')
      const userfound = registeredUsers.find(user => user.CellPhone === mobile.number);
      return userfound ? { ...contact, ID: userfound.ID } : contact;
      // incase you dont want `phoneContacts` without `ID`, you can just return `false` instead of `contact` and put `.filter(Boolean)` after the `.map()`
    });
    
    console.log(contactsMergerdWithID);

    【讨论】:

    • @bekkan,这是一个很简单的问题,随便玩弄一些常见的数组函数,你可以去看看MDN,他们有很好的文档。
    【解决方案2】:

    您可以使用 map 并查找,然后解构生成的对象

    const mapped = phoneContacts.map(e => {
      return {...e, ...{id: (registeredUsers.find(r => r.CellPhone === (e.phoneNumbers.find(p => p.label === 'mobile') || {number: -1}).number ) || {ID: 0}).ID}} ;
    });
    console.log(mapped);
    <script>
    const phoneContacts= [
      {
        firstName: "aaaa",
        lasttName: "aaaa",
        phoneNumbers: [{id: "1", label: "mobile", number: "09121111111"},{id: "1", label: "home", number: "02188888888"}]
      },
      {
        firstName: "bbbb",
        lasttName: "bbbb",
        phoneNumbers: [{id: "1", label: "mobile", number: "09122222222"},{id: "1", label: "home", number: "02177777777"}],
      },
    ]
    
    const registeredUsers= [
      {
        ID: 1,
        CellPhone: "09123333333"
      },
      {
        ID: 2,
        CellPhone: "09121111111"
      },
      ];
    
    </script>

    【讨论】:

    • 编辑了答案@bekkan
    猜你喜欢
    • 2021-10-24
    • 2022-11-30
    • 2020-08-15
    • 2019-05-29
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多