Как отфильтровать элемент по свойствам? - PullRequest
0 голосов
/ 29 мая 2019

Я хотел реорганизовать фильтр использования кода вместо lodash _forEach, код фильтра ниже не возвращает ожидаемый ответ. Есть идеи, что здесь реализовано неправильно?

main.js

const response = [];
const data = [{
    "isBrand": true,
    "drugName": "Lipitor",
    "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": {}
  }
];
_.forEach(data, function(drug) {
  if (drug.retailPrice !== undefined || drug.mailPrice !== undefined) {
    response.push(drug);
  }
});

const filterItems = data.filter(item => item.retailPrice && item.mailPrice)

console.log(filterItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

ожидаемый результат

[{
            "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": {}
        }
    ];

Ответы [ 2 ]

0 голосов
/ 29 мая 2019

Вы можете просто сделать Array.forEach следующим образом:

data.forEach(d => (d.retailPrice || d.mailPrice) ? response.push(d) : null)

Так бы это выглядело так:

const response = [];
const data = [{
    "isBrand": true,
    "drugName": "Lipitor",
    "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": {}
  }
];

data.forEach(d => (d.retailPrice || d.mailPrice) ? response.push(d) : null)

const filterItems = data.filter(item => item.retailPrice && item.mailPrice)

console.log(filterItems);
0 голосов
/ 29 мая 2019

Это должно работать.

var objNotEmpty = function(obj){
    return Object.keys(obj).length > 0;
 },
 objHasPrice = function(obj){
    return obj.hasOwnProperty("mailPrice") && obj.hasOwnProperty("retailPrice");
 };


const filterItems = data.filter( item => return objHasPrice(item) && objNotEmpty(item.mailPrice) && objNotEmpty(item.retailPrice);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...