简体   繁体   中英

filter array of objects based on another array of objects and return merged array

I have an array of objects like this:

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"}],
},
...]

I want to filter it with an array like this:

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

and return this:

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"}]
},
...]

I want to return the first array with the matched ID field in the second array if any of it's mobile phoneNumbers matched the CellPhone in the second array. How can I do that ?

 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); 

You can use map and find, then destructure the resulting objects

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM