0

Trying to filter out object based on some logic below code is returning empty array any help will be appreciated trying to achieve following logic

1- If first Obj (firstFillIndicator=Y, acceptDigitalFirstFill=Y) and Second Object(firstFillIndicator = Y, acceptDigitalFirstFill=N). this object should be removed its falsy.

2- if first obj(firstFillIndicator=Y, acceptDigitalFirstFill=Y) and seond object (firstFillIndicator= N, acceptDigitalFirstFill=Y). this object should be removed its falsy.

3- if both objects have firstFillIndicator and acceptDigitalFirstFill have some values that would be truthy.

main.ts

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

data

 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"

              }
          ]
      }
    ]

expected result

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

            }
        ]
    },

];
6
  • Do not use reduce, you won't need it for this use case. Commented Feb 17, 2020 at 22:11
  • isValidOrder() doesn't ever return a value, so would always evaluate to false. Commented Feb 17, 2020 at 22:13
  • Why use reduce here? You can but you should also be returning a value for the accumulator each time - you're not doing that. You're just comparing two objects at a time and...that's it. Commented Feb 17, 2020 at 22:14
  • 1
    Try using filter instead of reduce Commented Feb 17, 2020 at 22:14
  • I'm actually surprised this doesn't throw errors. You should get them for any length over 2, as you try to dereference properties off a which will be a boolean value. Commented Feb 17, 2020 at 22:16

1 Answer 1

1

The problem is that the isValidOrder function doesn't return anything, in any case, using something like .every(..) is more appropriate then using .reduce(..) in this case, here is an example:

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the example thats what i was looking addition to that , is it possible to capture the indexID of the removed object ?
@hussain You can get all the removed objects using something like this: const removed = rxDetails.filter(o => !result.some(v => v. indexID === o.indexID)) and then, you can map this array to only keep the IDs const ids = removed.map(o => o.indexID).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.