我正在尝试从我的data中获取有效和无效的数组,我们如何使用过滤器来完成这两个操作,以提供与条件匹配的有效数组,反之亦然。

数据

   const misMatchedItems = [];
      const matchedItems = [];
       const rxInfos=  [{
                "drugName": "ATRIPLA TABS",
                "ancillaryProductInd": "false",
                "firstFillIndicator": "N",
                "indexId": "1",
                "uniqueRxId": "1711511459709"
            },
            {
                "errorDetails": {
                    "errorCode": "0077",
                    "errorDesc": "uniqueRxId not found for2711511911555"
                }
            }
        ]

    const validArray = rxInfos.filter((element) => {
                    return (element.hasOwnProperty('indexId'));
                });
    matchedItems = validArray;
    const inValidArray = rxInfos.filter((element) => {
                    return (element.hasOwnProperty(!'indexId'));
                });

    misMatchedItems = inValidArray;

最佳答案

您在错误的位置有否定(感叹号)。我相信这应该有效:

const inValidArray = rxInfos.filter((element) => {
            return !(element.hasOwnProperty('indexId'));
        });


您还可以一次通过两个动作:

const validArray = [];
const invalidArray = [];

rxInfos.forEach(function(element) {
   if (element.hasOwnProperty('indexId')) {
       validArray.push(element);
   } else {
       invalidArray.push(element);
   }
});

10-08 09:44
查看更多