gpt4 book ai didi

javascript - Lodash 在其他数组项中搜索数组项

转载 作者:行者123 更新时间:2023-12-03 03:55:55 26 4
gpt4 key购买 nike

您好,我正在使用 lodash 库。

我想搜索receedMessagesMock中的电话是否在contactsMocks电话中

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

类似

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

contactMock 和 receiveMessagesMock

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

最佳答案

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

关于javascript - Lodash 在其他数组项中搜索数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44954549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com