2017-07-06 108 views
0

你好,我正在使用lodash庫。Lodash在其他數組項目中搜索數組項目

我想搜索,如果手機在receivedMessagesMock是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 
    } 
    ]; 
+0

問題是/? – Andreas

回答

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

你節省了我的一天@JoelCDoyle。感謝您的時間:) –

+0

嗨再次@JoelCDoyle 如何返回phoneNumber,如果pn.value!= rmm.phone;返回pn.value? –

+0

我不確定您的意思是「返回電話號碼」。 'pn'只在其定義的'lodash.find'謂詞函數的上下文內。該函數的返回語句將被評估爲布爾值,以確定它是否匹配,僅此而已。 –

相關問題