其他数组项

时间:2017-07-06 16:34:49

标签: javascript angularjs arrays search find

您好,我正在使用lodash库。

我想搜索receiveMessagesMock中的手机是否在contactsMocks手机中

如果receivedMessagesMock.phone === contactsMocks.phoneNumbers.value我需要将contactsMocks.name.formatted推送到相同的receivedMessagesMock位置。

这样的东西
if(receivedMessagesMock[0].phone === contactsMocks.phoneNumbers.value {
 receivedMessagesMock[0].name = contactsMocks.name.formatted;
}

contactMock和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 个答案:

答案 0 :(得分:1)

这是使用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;
});

请记住,您要求在收到的邮件中插入名称(单数)。这意味着如果列表中的两个或多个联系人具有相同的号码,无论出于何种原因,上述将始终返回第一个匹配。但是很可能会有多个匹配,因此插入一个名称数组可能是合适的。

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