foreach没有遍历元素吗?

时间:2019-01-25 21:45:06

标签: javascript arrays typescript foreach

如果没有与数据匹配的rxNumber如果没有按原样返回新字段payAmount,则在rxDetails上循环,因此在我的forEach下面,它没有返回5个元素,因此始终缺少rxNumber {作为回报,{1}}知道在这里实施错了什么吗?我注意到的是forEach正在跳过其中一个元素的循环,不确定为什么。

数据

15131503

main.ts

const rxDetails = [
  {
    "drugName": "TRILIPIX 135MG CPDR",
    "rxNumber": "15131523",
    "lldIndicator": "N"
  },
  {
    "drugName": "GILENYA 0.5MG CAPS",
    "rxNumber": "15131519",
    "lldIndicator": "N"
  },
  {
    "drugName": "JAKAFI 5MG TABS",
    "rxNumber": "15131503",
    "lldIndicator": "Y"
  },
  {
    "drugName": "FENOFIBRATE MICRONIZED 134MG CAPS",
    "rxNumber": "15131510",
    "lldIndicator": "N"
  },
  {
    "drugName": "LIPITOR 10MG TABS",
    "rxNumber": "15131506",
    "lldIndicator": "N"
  },
  {
    "drugName": "KEFLEX 750MG CAPS",
    "rxNumber": "15131522",
    "lldIndicator": "N"
  }
]

const data = [{
        "drugName": "TRILIPIX 135MG CPDR",
        "rxNumber": "15131523",
        "lldIndicator": "N",
        "payAmount": "10"
    },
    {
        "drugName": "GILENYA 0.5MG CAPS",
        "rxNumber": "15131519",
        "lldIndicator": "N",
        "payAmount": "8"
    },
    {
        "drugName": "METFORMIN",
        "rxNumber": "15425789",
        "lldIndicator": "Y",
        "payAmount": "0.50"
    },
    {
        "drugName": "FENOFIBRATE MICRONIZED 134MG CAPS",
        "rxNumber": "15131510",
        "lldIndicator": "N",
        "payAmount": "2.56"
    },
    {
        "drugName": "LIPITOR 10MG TABS",
        "rxNumber": "15131506",
        "lldIndicator": "N",
        "payAmount": "7.76"
    },
    {
        "drugName": "KEFLEX 750MG CAPS",
        "rxNumber": "15131522",
        "lldIndicator": "N",
        "payAmount": "17.88"
    }
]

预期输出

private getDrugsLastPrice(rxDetails: any, data: any) {
        let isDrugFound: boolean = false;
        const drugsArray: any = [];
        rxDetails.forEach((item: any) => {
            for (const element of data) {
                    if (item.rxNumber === element.rxNumber) {
                        isDrugFound = true;
                        const singleDrug = {
                            rxNumber: item.rxNumber,
                            lldIndicator: item.lldIndicator,
                            drugName: item.drugName,
                            payAmount: element.payAmount
                        };
                        drugsArray.push(singleDrug);
                    }

             }
            if (!isDrugFound) {
                drugsArray.push(item);
            }

        });

        return drugsArray;

    }

getDrugsLastPrice(rxDetails,data);

2 个答案:

答案 0 :(得分:1)

您遇到的主要问题是需要在isDrugFound循环中设置forEach的声明。设置为 true 后,它会保持 true ,这就是问题所在。

我简化了代码,因此您不需要布尔值。

使用Array#map,Array#find,扩展语法和解构。

const rxDetails=[{"drugName":"TRILIPIX 135MG CPDR","rxNumber":"15131523","lldIndicator":"N"},{"drugName":"GILENYA 0.5MG CAPS","rxNumber":"15131519","lldIndicator":"N"},{"drugName":"JAKAFI 5MG TABS","rxNumber":"15131503","lldIndicator":"Y"},{"drugName":"FENOFIBRATE MICRONIZED 134MG CAPS","rxNumber":"15131510","lldIndicator":"N"},{"drugName":"LIPITOR 10MG TABS","rxNumber":"15131506","lldIndicator":"N"},{"drugName":"KEFLEX 750MG CAPS","rxNumber":"15131522","lldIndicator":"N"}]
const data=[{"drugName":"TRILIPIX 135MG CPDR","rxNumber":"15131523","lldIndicator":"N","payAmount":"10"},{"drugName":"GILENYA 0.5MG CAPS","rxNumber":"15131519","lldIndicator":"N","payAmount":"8"},{"drugName":"METFORMIN","rxNumber":"15425789","lldIndicator":"Y","payAmount":"0.50"},{"drugName":"FENOFIBRATE MICRONIZED 134MG CAPS","rxNumber":"15131510","lldIndicator":"N","payAmount":"2.56"},{"drugName":"LIPITOR 10MG TABS","rxNumber":"15131506","lldIndicator":"N","payAmount":"7.76"},{"drugName":"KEFLEX 750MG CAPS","rxNumber":"15131522","lldIndicator":"N","payAmount":"17.88"}]

const res = rxDetails.map(item=>{
  const d = data.find(({rxNumber})=>rxNumber===item.rxNumber);
  return d ? {...item, payAmount: d.payAmount} : {...item};
});

console.log("Number of elements: " + res.length);
console.log("Number with payAmount prop: " + res.filter(o=>o.payAmount !== undefined).length);
console.log("Number without payAmount prop:" + res.filter(o=>o.payAmount === undefined).length);
console.log(res);

答案 1 :(得分:0)

您永远不会在isDrugFound循环中重置forEach。找到匹配项后,下一次迭代仍将isDrugFound设置为true,因此除非它也恰好位于数据数组中,否则不会推送该迭代。实际上,一旦找到一个数据匹配项,就会仅推送数据数组中的元素。