有没有办法在 es6 过滤器或 lodash 中获得相同的结果?

侯赛因

尝试根据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"
        }
    }
]

转换.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"
    }
  }
]
谢尔盖·纳罗日尼

你的意思是这样?

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有没有办法在咖喱函数中参数化过滤器评估的右侧

来自分类Dev

有没有办法在RestAssured中强制过滤器顺序?

来自分类Dev

ES6:先过滤再映射->是否有办法从过滤器中查找元素数量?

来自分类Dev

有没有办法在Warp中作为过滤器的一部分进行验证?

来自分类Dev

有没有办法在javascript es6中更改'this'的css属性?

来自分类Dev

带有lodash的过滤器数组

来自分类Dev

过滤器中的 Lodash 和 OR 逻辑

来自分类Dev

数组中的 lodash 过滤器

来自分类常见问题

有没有办法在vscode中打开ES6 / ES7语法支持?

来自分类Dev

有没有办法使发布的过滤器成为“安全过滤器”

来自分类Dev

有没有办法让算术运算符在Javascript ES6中使用getter和setter?

来自分类Dev

具有功能的树中的 Lodash 和 deepdash 过滤器

来自分类Dev

有没有办法从 Yii2 的前一个过滤器中获取一个变量?

来自分类Dev

使用简单的映射和过滤器 es6 过滤现有的对象数组

来自分类Dev

具有限制的Lodash过滤器

来自分类Dev

带有多重检查的lodash动态过滤器

来自分类Dev

带有lodash的嵌套集合过滤器

来自分类Dev

有没有办法为所有操作为 CancellationToken 创建过滤器?

来自分类Dev

有没有办法计算(评论)以获得正确的总数,然后过滤 WHERE comment =?

来自分类Dev

Lodash 过滤器搜索数组中的几个元素(Vue)

来自分类Dev

有没有办法查询消息过滤器是否已经生效?

来自分类Dev

有没有办法在libreoffice calc中保存过滤器?

来自分类Dev

Dstore 过滤器:有没有办法比较属性?

来自分类Dev

有没有办法在 ffmpeg 中使用外部过滤器?

来自分类Dev

有没有办法从MVC过滤器再次运行请求

来自分类Dev

有没有办法在Python-Pandas中多重过滤Dataframe?

来自分类Dev

有没有办法在CouchDB中过滤子文档?

来自分类Dev

有没有办法在python中的两个数据行之间进行过滤?

来自分类Dev

有没有办法使用cmder过滤Windows中的命令历史记录?

Related 相关文章

  1. 1

    有没有办法在咖喱函数中参数化过滤器评估的右侧

  2. 2

    有没有办法在RestAssured中强制过滤器顺序?

  3. 3

    ES6:先过滤再映射->是否有办法从过滤器中查找元素数量?

  4. 4

    有没有办法在Warp中作为过滤器的一部分进行验证?

  5. 5

    有没有办法在javascript es6中更改'this'的css属性?

  6. 6

    带有lodash的过滤器数组

  7. 7

    过滤器中的 Lodash 和 OR 逻辑

  8. 8

    数组中的 lodash 过滤器

  9. 9

    有没有办法在vscode中打开ES6 / ES7语法支持?

  10. 10

    有没有办法使发布的过滤器成为“安全过滤器”

  11. 11

    有没有办法让算术运算符在Javascript ES6中使用getter和setter?

  12. 12

    具有功能的树中的 Lodash 和 deepdash 过滤器

  13. 13

    有没有办法从 Yii2 的前一个过滤器中获取一个变量?

  14. 14

    使用简单的映射和过滤器 es6 过滤现有的对象数组

  15. 15

    具有限制的Lodash过滤器

  16. 16

    带有多重检查的lodash动态过滤器

  17. 17

    带有lodash的嵌套集合过滤器

  18. 18

    有没有办法为所有操作为 CancellationToken 创建过滤器?

  19. 19

    有没有办法计算(评论)以获得正确的总数,然后过滤 WHERE comment =?

  20. 20

    Lodash 过滤器搜索数组中的几个元素(Vue)

  21. 21

    有没有办法查询消息过滤器是否已经生效?

  22. 22

    有没有办法在libreoffice calc中保存过滤器?

  23. 23

    Dstore 过滤器:有没有办法比较属性?

  24. 24

    有没有办法在 ffmpeg 中使用外部过滤器?

  25. 25

    有没有办法从MVC过滤器再次运行请求

  26. 26

    有没有办法在Python-Pandas中多重过滤Dataframe?

  27. 27

    有没有办法在CouchDB中过滤子文档?

  28. 28

    有没有办法在python中的两个数据行之间进行过滤?

  29. 29

    有没有办法使用cmder过滤Windows中的命令历史记录?

热门标签

归档