Javascript 如何从数组中获取不匹配的元素?
所以我收到了一个请求,其中包含了rxInfos和我从缓存中获得的特殊成员,所以如果rxInfos.indexID的匹配项在那里,我会进行一些处理,如果rxInfos indexID在那里,但由于某些原因与缓存indexID不同,我想将该indexID推送到不匹配的索引数组中。与第二个元素类似的是rxInfos.indexID与任何特殊成员都不匹配,因此应将其推送到不匹配的索引中 使用下面的代码,它将所有特殊成员ID推送到不匹配的数组中 梅因酒店Javascript 如何从数组中获取不匹配的元素?,javascript,arrays,angular,typescript,Javascript,Arrays,Angular,Typescript,所以我收到了一个请求,其中包含了rxInfos和我从缓存中获得的特殊成员,所以如果rxInfos.indexID的匹配项在那里,我会进行一些处理,如果rxInfos indexID在那里,但由于某些原因与缓存indexID不同,我想将该indexID推送到不匹配的索引数组中。与第二个元素类似的是rxInfos.indexID与任何特殊成员都不匹配,因此应将其推送到不匹配的索引中 使用下面的代码,它将所有特殊成员ID推送到不匹配的数组中 梅因酒店 for (const member of spe
for (const member of specialMembers) {
for (const rxInfo of this.rxInfos) {
if (member.indexID === rxInfo.indexID) {
this.indexIDs.push(rxInfo.indexID);
proxyMember = member;
if (!member.dateOfBirth) {
statusDesc = "member dateOfbirth not found";
return Promise.reject(this.errorHandler(request, statusDesc));
}
const requestBody: any = this.buildSingleRequestBody(proxyMember, rxInfo);
const requestObject = this.specialtyQuestionRequest(requestBody);
this.requestArray.push(requestObject);
} else {
this.mismatchIndexIDS.push(rxInfo.indexID);
this.indexIdMismatchCounter++;
}
}
}
资料
有一种更好的方法,可以直接从
specialMembers
获取成员,并使用特定的indexID
。此外,您只需为循环处理一个(另一个在find
中隐含):
for
循环的简化版本如下:
var rxInfos=[{
“drugNdc”:“10101”,
“rxNumber”:“14556459709”,
“firstFillIndicator”:“N”,
“源系统”:“TBS”,
“indexID”:“RPT0ifQ”
},
{
“drugNdc”:“101”,
“rxNumber”:“145945000709”,
“firstFillIndicator”:“N”,
“源系统”:“TBS”,
“indexID”:“GJhQ1MrQnZkTFRR”
}
];
变量特殊成员=[{
“dob”:“12-12-1970”,
“名字”:“吉米”,
“lasteName”:“SHOW”,
“indexID”:“RPT0ifQ”
},
{
“dob”:“18-10-1970”,
“名字”:“蒂米”,
“lasteName”:“Doug”,
“indexID”:“GJhQ1MrQ”
},
{
“dob”:“17-06-1981”,
“名字”:“约翰”,
“lasteName”:“owascar”,
“indexID”:“GJhQ1MrTGDSRQ”
}
];
var indexIDs=[],
不匹配的HindExids=[],
INDEXIDMASCHCOUNTER=0;
用于(rxInfos的常数rxInfo){
const member=specialMembers.find(member=>member.indexID==rxInfo.indexID);
国际单项体育联合会(成员){
推挤(rxInfo.indexID);
console.log('进程',成员);
}否则{
不匹配的hindexids.push(rxInfo.indexID);
indexidmistchcounter++;
}
}
log('index id',indexIDs);
log('mismatches',mistchindexids);
console.log('counter',indexidmistchcounter)代码>我还想得到member元素,这样我可以进一步处理逻辑,就像在我的代码中,我必须将member分配给proxymber一样,在这种情况下,我必须使用filter ithink@hussain我将答案改为使用find
。这样,您就可以获得给定ID的成员。
"rxInfos": [
{
"drugNdc": "10101",
"rxNumber": "14556459709",
"firstFillIndicator": "N",
"sourceSystem": "TBS",
"indexID": "RPT0ifQ"
},
{
"drugNdc": "101",
"rxNumber": "145945000709",
"firstFillIndicator": "N",
"sourceSystem": "TBS",
"indexID": "GJhQ1MrQnZkTFRR"
}
]
"specialyMembers":[
{
"dob":"12-12-1970"
"firstName": "jimmy",
"lasteName": "shew",
"indexID": "RPT0ifQ"
},
{
"dob":"18-10-1970"
"firstName": "Timmy",
"lasteName": "Doug",
"indexID": "GJhQ1MrQ"
},
{
"dob":"17-06-1981"
"firstName": "John",
"lasteName": "owascar",
"indexID": "GJhQ1MrTGDSRQ"
}
]
specialMembers.find(member => member.indexID === rxInfo.indexID)
const memberMatched = member => {
return rxInfos.find(rxInfo => rxInfo.indexID === member.indexID)
}
const mismatchIndexIDS = specialMembers.reduce((res, member) => {
return memberMatched(member) ? res : res.concat(member.indexID)
}, [])
const indexIdMismatchCounter = mismatchIndexIDS.length
const matchedMembers = specialMembers.reduce((res, member) => {
const rxRecord = memberMatched(member)
return rxRecord ? res.concat({rxRecord, member}) : res
}, [])
// DO YOUR STUFF WITH MATCHED MEMBERS