如何从数组中获取不匹配的元素?

时间:2018-11-28 00:31:58

标签: javascript arrays angular typescript

所以我有从缓存中获取的具有rxInfos和specialMembers的请求,因此如果rxInfos.indexID匹配,则进行其他处理(如果rxInfos indexID存在),但由于某种原因与缓存indexID不同,我想将该indexID推送到mismatchedIndexIDs数组。就像第二个元素一样,rxInfos.indexID与任何specialMembers不匹配,因此应推送到mismatchedIndexIDs。

使用以下代码将所有specialMembers id推送到不匹配的数组。

main.ts

@ServiceActivator

数据

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

有一种更好的方法可以使用find进行操作,它可以直接从specialMembers获得具有特定indexID的成员。此外,您只需要处理一个for循环(另一循环在find中是隐式的):

specialMembers.find(member => member.indexID === rxInfo.indexID)

for循环的简化版本如下:

var rxInfos = [{
    "drugNdc": "10101",
    "rxNumber": "14556459709",
    "firstFillIndicator": "N",
    "sourceSystem": "TBS",
    "indexID": "RPT0ifQ"
  },
  {
    "drugNdc": "101",
    "rxNumber": "145945000709",
    "firstFillIndicator": "N",
    "sourceSystem": "TBS",
    "indexID": "GJhQ1MrQnZkTFRR"
  }
];

var specialMembers = [{
    "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"
  }
];

var indexIDs = [],
  mismatchIndexIDS = [],
  indexIdMismatchCounter = 0;


for (const rxInfo of rxInfos) {
  const member = specialMembers.find(member => member.indexID === rxInfo.indexID);
  if (member) {
    indexIDs.push(rxInfo.indexID);
    console.log('process', member);
  } else {
    mismatchIndexIDS.push(rxInfo.indexID);
    indexIdMismatchCounter++;
  }
}

console.log('index ids', indexIDs);
console.log('mismatches', mismatchIndexIDS);
console.log('counter', indexIdMismatchCounter);