0

Hello i'm using lodash library.

I want to search if phone in receivedMessagesMock is in contactsMocks phone

If receivedMessagesMock.phone === contactsMocks.phoneNumbers.value I need to push the contactsMocks.name.formatted in the same receivedMessagesMock position.

Something like

if(receivedMessagesMock[0].phone === contactsMocks.phoneNumbers.value {
 receivedMessagesMock[0].name = contactsMocks.name.formatted;
}

The contactMock and receivedMessagesMock

var contactsMock = [{'id':'1','rawId':'1','displayName':'Asd','name':{'givenName':'Asd','formatted':'Asd'},'nickname':null,'phoneNumbers':[{'id':'1','pref':false,'value':'000000000','type':'mobile', 'loggedInSystem':true}],'emails':null,'addresses':null,'ims':null,'organizations':null,'birthday':null,'note':null,'photos':null,'categories':null,'urls':null},{'id':'2','rawId':'2','displayName':'Bbb','name':{'givenName':'Bbb','formatted':'Bbb'},'nickname':null,'phoneNumbers':[{'id':'3','pref':false,'value':'565 65 65 65','type':'mobile'}],'emails':null,'addresses':null,'ims':null,'organizations':null,'birthday':null,'note':null,'photos':null,'categories':null,'urls':null},{'id':'3','rawId':'3','displayName':'Ccc','name':{'givenName':'Ccc','formatted':'Ccc'},'nickname':null,'phoneNumbers':[{'id':'5','pref':false,'value':'0000000001','type':'mobile'}],'emails':null,'addresses':null,'ims':null,'organizations':null,'birthday':null,'note':null,'photos':null,'categories':null,'urls':null},{'id':'4','rawId':'4','displayName':'Ddd','name':{'givenName':'Ddd','formatted':'Ddd'},'nickname':null,'phoneNumbers':[{'id':'6','pref':false,'value':'000 00 00 01','type':'mobile'}],'emails':null,'addresses':null,'ims':null,'organizations':null,'birthday':null,'note':null,'photos':null,'categories':null,'urls':null}];

    var receivedMessagesMock = [{
        'id': 12,
        'phone': '000 00 00 01',
        'time': '15:44',
        'priority': 1,
        'response' : false
    },{
        'id': 15,
        'phone': '000 00 00 01',
        'time': '15:44',
        'priority': 1,
        'response' : false
    },{
        'id': 16,
        'phone': '000 00 00 01',
        'time': '15:44',
        'priority': 2,
        'response' : true
    }
    ];
1
  • And the question/problem is? Commented Jul 6, 2017 at 16:40

1 Answer 1

1

This is one way to do this using lodash:

lodash.map(receivedMessagesMock, function(rmm) {
  var foundContact = lodash.find(contactsMock, function(cm) {
    return lodash.find(cm.phoneNumbers, function(pn) {
      return pn.value == rmm.phone;
    });
  });
  if (foundContact) {
    rmm.names = foundContact.name.formatted
  }
  return rmm;
});

Keep in mind, you asked for a name (singular) to be inserted into the received messages. This means that if two or more contacts in the list have the same number, for whatever reason, the above will always return the first match. But there is a real possibility that there will be more than one match, so it might be appropriate to insert an array of names.

lodash.map(receivedMessagesMock, function(rmm) {
  var foundContacts = lodash.filter(contactsMock, function(cm) {
    return lodash.find(cm.phoneNumbers, function(pn) {
      return pn.value == rmm.phone;
    });
  });
  if (foundContacts.length > 0) {
    rmm.names = lodash.map(foundContacts, function(fcs) {
      return name.formatted
    });
  }
  return rmm;
});
Sign up to request clarification or add additional context in comments.

4 Comments

You save my day @JoelCDoyle. thanks for your time :)
Hi again @JoelCDoyle How can I return the phoneNumber if pn.value != rmm.phone; return pn.value ¿?
I'm not sure what you mean by "return the phoneNumber". pn is only in scope inside the context of the lodash.find predicate function that it is defined in. The returned statement of that function will be evaluated as a boolean to determine if it is a match, nothing more.
When pn.value == rmm.phone is not true I want that the 'name.formatted' = to pn.value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.