【问题标题】:is there way to get same result in es6 filter or lodash?有没有办法在 es6 过滤器或 lodash 中获得相同的结果?
【发布时间】:2019-05-22 18:15:13
【问题描述】:

尝试根据rejectMessage 数组中的settlementCode 从数组中删除元素,我觉得使用ES6 或lodash 会好很多。

有人可以帮我使用这种方法吗?

数据

const data = [
  {
        "drugName": "Metformin",
        "mailPrice": {
            "copayEmployer": "N/A",
            "totalQuantity": "90.0",
            "rejectMessage": [{
                "settlementCode": "99",
                "settlementDesc": "Not Covered: Call us - System could not process your request. Call us at the toll-free number on your benefit ID card.||Sin cobertura: Llámenos - El sistema no pudo procesar su solicitud. Llame al número gratuito que figura en su tarjeta de identificación de beneficios."
            }]
        },
        "retailPrice": {
            "copayEmployer": "N/A",
            "totalQuantity": "30.0"
        }
    },
    {
        "drugName": "CALCIUM",
        "mailPrice": {
            "copayEmployer": "N/A",
            "totalQuantity": "90.0"
        },
        "retailPrice": {
            "copayEmployer": "N/A",
            "totalQuantity": "30.0"
        }
    }
]

transform.js

    function transformPrice(drugPrice) {

          if (drugPrice.retailPrice.rejectMessage.length || drugPrice.mailPrice.rejectMessage.length ){
            const retailRejecrMsg = drugPrice.retailPrice.rejectMessage;
            const mailRejectMsg = drugPrice.mailPrice.rejectMessage;
            const retailErr = isErrorPresent(retailRejecrMsg);
            const mailErr =  isErrorPresent(mailRejectMsg);
          }

          if(retailErr){
            delete drugPrice.retailPrice;
          }

          if( mailErr){
            delete drugPrice.mailPrice;
          }

          return drugPrice;
        }

        function isErrorPresent (price) {
          const isError = function (element) {
            const bRet = checkErrorCodes(element);
            return (element.hasOwnProperty('settlementCodes') && bRet)
          }

          return price.some(isError);
        }

        function checkErrorCodes(el){
          let bRet = false;
          const errorCodes = [
            10015,
            2356,
            225,
            224,
              99
          ] 
          for (const err of errorCodes){

            if (err === ele.settlementCode){

              bRet = true;
            }
          }
           return bRet;
        }

transformPrice(data);

预期结果

[{
    "drugName": "Metformin",
    "retailPrice": {
      "copayEmployer": "N/A",
      "totalQuantity": "30.0"
    }
  },
  {
    "drugName": "CALCIUM",
    "mailPrice": {
      "copayEmployer": "N/A",
      "totalQuantity": "90.0"
    },
    "retailPrice": {
      "copayEmployer": "N/A",
      "totalQuantity": "30.0"
    }
  }
]

【问题讨论】:

  • “我觉得在 ES6 时代我写了很多代码”.. 你是从未来发布的吗?
  • _.remove 如果您需要就地删除,或者_.filter/.filter 如果您可以重新分配阵列。
  • 我没有看到任何引用数据的代码。您忘记发布该代码了吗?
  • 这个输入的预期输出是什么? “根据rejectMessage数组中的settlementCode从数组中删除元素”具体基于什么值?
  • @adiga 如果对象中的结算代码与我在 checkErrorCode 方法中的错误代码匹配

标签: javascript ecmascript-6 lodash


【解决方案1】:

你的意思是这样的?

import _ from 'lodash';

const drugPrice = data.map(item => {
  const errorCodes = [10015, 2356, 225, 224, 99];
  const f = ["mailPrice.rejectMessage", "retailPrice.rejectMessage"];

  f.forEach(path => {
    const rejectMsg = _.get(item, path);

    if (rejectMsg) {
      const y = path.split(".").shift();
      const hasCodeOrWrong = rejectMsg.find(i => {
        return !!~errorCodes.indexOf(+i.settlementCode)
            || !!~i.settlementDesc.indexOf(':');
      });

      hasCodeOrWrong && delete item[y];
    }
  });

  return item;
});

【讨论】:

  • 如果药物零售或邮件价格具有rejectMessage数组并且它包含那些零售/邮件价格应删除的错误代码,则对象将是这样的。 [ { "drugName": "Metformin", "mailPrice": { "copayEmployer": "N/A", "totalQuantity": "90.0", "rejectMessage": [{ "settlementCode": "99", "settlementDesc": "Not Covered: Call us -os." }] }, "retailPrice": { "copayEmployer": "N/A", "totalQuantity": "30.0" } } ]
  • @hussain,上面的代码按照你的描述工作
  • 是否可以在此代码之上添加另一个逻辑f the "settlementDesc" in rejectMessage, has a string without ":" , omit that entry of "rejectMessage"If all entries are omitted in the "rejectMessage", then remove the "rejectMessage" entry from the drug price response.
  • @hussain,再一次。如果内部没有“:”,您想删除“settlementDesc”属性。如果它是空数组,则删除“rejectMessage”属性?
  • 一个更正我想删除那个rejectMessage对象如果settlementDesc是“:”那个条目应该从rejectMessage数组中删除
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多