如何从数组中找到匹配的元素并进行进一步处理?

时间:2018-12-06 19:30:50

标签: javascript arrays angular typescript

我正在为后端构建请求,因此尝试创建两个数组,一个与请求中的indexId相匹配,另一个与不匹配。这样我就可以分别调用this.buildSingleRequestBody(proxyMember, matchingArray)

来建立两个数组的请求

main.ts

   for (const rxInfo of this.rxInfos) {
        const member = specialtyMembers.find((memberData) => memberData.indexID ===
            rxInfo.indexID);
        if (member) {
            this.indexIDs.push(rxInfo.indexID);
            proxyMember = member;
            if (!member.dateOfBirth) {
                statusDesc = "member dateOfbirth not found";
                return Promise.reject(this.errorHandler(request, statusDesc));
            }
            const tempArray: any = this.rxInfos;
            const matchingArray: any = [];
            const unmatched: any = [];
            tempArray.forEach((element: any) => {
                if (element.indexID === rxInfo.IndexID) {
                    matchingArray.push(rxInfo);
                } else {
                  unmatched.push(rxInfo) 
                }
            })
// this method should call for both matchingArray and unmatchedArray 
            const requestBody: any = this.buildSingleRequestBody(proxyMember, matchingArray);
            const requestObject = this.specialtyQuestionRequest(requestBody);
            this.requestArray.push(requestObject);
        } else {
            this.mismatchIndexIDS.push(rxInfo.indexID);
            this.indexIdMismatchCounter++;
        }
    }

  private buildSingleRequestBody(specialtyMember: any, rxInfo: any) {

        return {
            "specialtyID":"1234",
            "sourceSystem": "test",
            "rxInfos": rxInfo,
            "dateOfBirth": "1956-07-12",

        };
    }

数据

{
    "rxInfos": [
        {
            "drugNdc": "00004080085",
            "rxNumber": "1459004",
            "indexID":"HGYIIITTS=="
        },
            {
            "drugNdc": "00004080085",
            "rxNumber": "144500004",
            "indexID":"HGYIIITTS=="
        },
        {
            "drugNdc": "00004080085",
            "rxNumber": "1677004",
            "indexID":"HGTHJJSKK=="
        }
    ]
}

buildSingleRequestBody为每个数组的输出

unmatched request {
    "specialtyID": "1234",
    "sourceSystem": "test",
    "dateOfBirth": "1956-07-12",
    "rxInfos": [{
            "drugNdc": "00004080085",
            "rxNumber": "1677004",
            "indexID": "HGTHJJSKK=="
        }

    ]
}

matched array requerst 
{
    "specialtyID": "1234",
    "sourceSystem": "test",
    "dateOfBirth": "1956-07-12",
    "rxInfos": [{
            "drugNdc": "00004080085",
            "rxNumber": "1459004",
            "indexID": "HGYIIITTS=="
        },
        {
            "drugNdc": "00004080085",
            "rxNumber": "144500004",
            "indexID": "HGYIIITTS=="
        }
    ]
}

0 个答案:

没有答案