如何在JS中删除属性并映射新对象?

时间:2019-05-28 18:34:57

标签: javascript arrays ecmascript-6

想要删除具有匹配errorCode的属性并在完成所有过滤后转换新对象,我不会低于预期的响应率吗?下面的代码是什么问题?

如果解决方案代码与drugPriceErrors匹配,则findErrCode应该返回true。

main.js

const data = [{
            "isBrand": true,
            "drugName": "Lipitor",
            "mailPrice": {
                "totalQuantity": 90,
                "rejectMessage": [{
                    "settlementCode": "99",
                    "settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
                }]
            },
            "retailPrice": {
                "totalQuantity": 30,
                "rejectMessage": [{
                    "settlementCode": "85",
                    "settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
                }]
            },
            "specialtyPrice": {}
        },
        {
            "isBrand": false,
            "drugName": "Atorvastatin Calcium",
            "drugStrength": "80mg",
            "drugForm": "Tablet",
            "mailPrice": {
                "totalQuantity": 90,
                "rejectMessage": [{
                    "settlementCode": "99",
                    "settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
                }]
            },
            "retailPrice": {
                "totalQuantity": 30,
                "rejectMessage": [{
                    "settlementCode": "99",
                    "settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
                }]
            },
            "specialtyPrice": {}
        }
    ];

const drugPricesErrors = [99,45,89];

function filterDrugs (data) { 
  const response = [];
const _drugPrice = data.map(item => {
        const retailHasCode = findErrCode(item.retailPrice.rejectMessage);
        const mailHasCode = findErrCode (item.mailPrice.rejectMessage);

        if(retailHasCode) {
            delete item.retailPrice;
        }

        if(mailHasCode) {
            delete item.mailPrice;
        }

  return item;

    });

    _.forEach(_drugPrice, function(drug) {
        if(drug.retailPrice !== undefined || drug.mailPrice !== undefined){
            response.push(drug);
        }
    });

  return response;
}

function findErrCode(data) {
    const filterMessage = data.filter( message => drugPriceErrors.includes(item.settlementCode));
    return filterMessage;
}

预期输出

[{
            "isBrand": true,
            "drugName": "Lipitor",
            "retailPrice": {
                "totalQuantity": 30,
                "rejectMessage": [{
                    "settlementCode": "85",
                    "settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
                }]
            },
            "specialtyPrice": {}
        }
    ]

1 个答案:

答案 0 :(得分:2)

将您的#include <sys/stat.h> bool IsLocked(std::string& thePath) { bool isLocked = false; struct stat buf; int retval = stat(thePath.c_str(), &buf); // NOTE: retval could be an error, like ENOENT (file doesn't exist) if (0 != (buf.st_flags & UF_IMMUTABLE)) { isLocked = true; //is immutable } return isLocked; } 更新为以下内容:

findErrCode

关键是使用let findErrCode = data => data.some(i => drugPriceErrors.includes(Number(i.settlementCode))) 来转换您的Number,以便settlementCode起作用。也请使用Array.some,因为您只关心结果是true还是false(布尔值)。 Array.some实际上会返回记录等。

您可以看到它在here上运行