javascript - 过滤器认为具有辅助号码的联系人

标签 javascript react-native

我试图过滤并仅获取号码在 numbers 中的联系人,我的问题是一个号码可能是联系人的辅助号码,filter(indexOf(foreach() )) 似乎在这里不起作用,有什么建议吗?

const filteredContacts = contacts.filter(contact => numbers.indexOf(contact.phoneNumbers.forEach(phone => phone.number)) > -1);

//sample of contacts
Object {
 "company": "Financial Services Inc.",
 "contactType": "person",
 "firstName": "Hank",
 "id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
 "imageAvailable": false,
 "jobTitle": "Portfolio Manager",
 "lastName": "Zakroff",
 "middleName": "M.",
 "name": "Hank M. Zakroff",
 "phoneNumbers": Array [
 Object {
   "countryCode": "us",
   "digits": "5557664823",
   "id": "337A78CC-C90A-46AF-8D4B-6CC43251AD1A",
   "label": "work",
   "number": "(555) 766-4823",
 },
 Object {
   "countryCode": "us",
   "digits": "7075551854",
   "id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
   "label": "other",
   "number": "(707) 555-1854",
 },
],
},
Object {
  "contactType": "person",
  "firstName": "David",
  "id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
  "imageAvailable": false,
  "lastName": "Taylor",
  "name": "David Taylor",
  "phoneNumbers": Array [
    Object {
      "countryCode": "us",
      "digits": "5556106679",
      "id": "FE064E55-C246-45F0-9C48-822BF65B943F",
      "label": "home",
      "number": "555-610-6679",
    },
   ],
  },
 ]

//Sample of numbers
numbers = [
(707) 555-1854,
555-610-6679
]

//Expected
filteredContacts = //Both contacts

最佳答案

这应该适合你:

const contacts= [
  {
  company: "Financial Services Inc.",
  // ....
  phoneNumbers:[
  {
    //...
    number: "(555) 766-4823",
  },
  {
    //...
    number: "555-610-6679",
  }
  ]
  }
  //...
  
];
//Sample of numbers
const numbers = [
'(707) 555-1854',
'555-610-6679'
]


const filteredContacts = contacts.filter(contact => {
  let number_exists=false;
  contact.phoneNumbers.map(phone => phone.number).forEach( phoneNr =>{
   if(numbers.indexOf(phoneNr)>-1) number_exists=true;
   }
  );
  
 return number_exists;
  
  }
);




//Expected
console.log(filteredContacts)

关于javascript - 过滤器认为具有辅助号码的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54986933/

相关文章:

javascript - 如何在 Django 模板过滤器中用下划线替换逗号

asp.net - 如何使用 javascript 为 ajax 选项卡面板启用 css 属性

javascript - 当字符串包含 HTML 标记时 JSON 无法解析

javascript - 如何在 D3.js 中添加箭头链接?

javascript - 如何在 parseJson 函数中使用变量与字符串对象?

android - 在 windows 10 中找不到 tools.jar React Native Android

javascript - 同一依赖项的多个 React 效果 Hook

typescript - 运行 "tsc"是检查 "node_modules"中的一个文件

react-native - 如何将导航 Prop 传递给 "createStackNavigator"以使用带动画的默认标题

android - 如何在本地构建 Expo APK