尝试根据代码下方的某些逻辑过滤出对象,然后返回空数组,尝试实现以下逻辑将对您有所帮助

1-如果第一个对象(firstFillIndicator = Y,acceptDigitalFirstFill = Y)和第二个对象(firstFillIndicator = Y,acceptDigitalFirstFill = N)。这个物体应该被删除。

2-如果第一个obj(firstFillIndicator = Y,acceptDigitalFirstFill = Y)和第二个对象(firstFillIndicator = N,acceptDigitalFirstFill = Y)。这个物体应该被删除。

3-如果两个对象都具有firstFillIndicator和acceptDigitalFirstFill则具有一些真实的值。

主要

function validateSingleOrder(data){
  let isValid = false;
  const filteredDrugs = data.filter((item) => {
      isValid = isValidOrder(item);
      if(isValid){
        return true;
      } else {
        return false;
      }
  })

 return filteredDrugs;
}

function isValidOrder(item){
   item.rxInfos.reduce((a, c) => {
    return (a.acceptDigitalFirstFill === "Y" && c.firstFillIndicator === "Y") || (a.firstFillIndicator === "N" && c.acceptDigitalFirstFill === "N")
  });
}

console.log(validateSingleOrder(rxDetails));


数据

 const rxDetails = [
      {
            indexID: 1,
            rxInfos: [{
                    firstFillIndicator: "Y",
                    acceptDigitalFirstFill: "Y",
                    rxNumber: "1512"
                },
                {
                    firstFillIndicator: "Y",
                    acceptDigitalFirstFill: "N",
                    rxNumber: "16021"

                }
            ]
      },
      {
          indexID: 2,
          rxInfos: [{
                  firstFillIndicator: "Y",
                  acceptDigitalFirstFill: "Y",
                  rxNumber: "1512"
              },
              {
                  firstFillIndicator: "N",
                  acceptDigitalFirstFill: "Y",
                  rxNumber: "16021"

              }
          ]
      },
      {
          indexID: 3,
          rxInfos: [{
                  firstFillIndicator: "Y",
                  acceptDigitalFirstFill: "Y",
                  rxNumber: "1512"
              },
              {
                  firstFillIndicator: "N",
                  acceptDigitalFirstFill: "N",
                  rxNumber: "16021"

              }
          ]
      }
    ]


预期结果

[{
        indexID: 3
        rxInfoss: [{
                firstFillIndicator: "Y",
                acceptDigitalFirstFill: "Y",
                rxNumber: "1512"
            },
            {
                firstFillIndicator: "N",
                acceptDigitalFirstFill: "N",
                rxNumber: "16021"

            }
        ]
    },

];

最佳答案

问题在于,在任何情况下,isValidOrder函数都不会返回任何内容,在这种情况下,使用.every(..)之类的东西比使用.reduce(..)更合适,这是一个示例:



const rxDetails = [{
    indexID: 1,
    rxInfos: [{
        firstFillIndicator: "Y",
        acceptDigitalFirstFill: "Y",
        rxNumber: "1512"
      },
      {
        firstFillIndicator: "Y",
        acceptDigitalFirstFill: "N",
        rxNumber: "16021"

      }
    ]
  },
  {
    indexID: 2,
    rxInfos: [{
        firstFillIndicator: "Y",
        acceptDigitalFirstFill: "Y",
        rxNumber: "1512"
      },
      {
        firstFillIndicator: "N",
        acceptDigitalFirstFill: "Y",
        rxNumber: "16021"

      }
    ]
  },
  {
    indexID: 3,
    rxInfos: [{
        firstFillIndicator: "Y",
        acceptDigitalFirstFill: "Y",
        rxNumber: "1512"
      },
      {
        firstFillIndicator: "N",
        acceptDigitalFirstFill: "N",
        rxNumber: "16021"

      }
    ]
  }
];


const result = rxDetails.filter(o => {
  return o.rxInfos.every(v => {
    return (v.firstFillIndicator === 'Y' && v.acceptDigitalFirstFill === 'Y') || (v.firstFillIndicator === 'N' && v.acceptDigitalFirstFill === 'N');
  });
});

console.log(result);

关于javascript - 如何使用reduce从数组中删除元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60271056/

10-09 22:47