1

I have a problem on how to remove specific param that is passed as payload whenever the value is empty or null

Here is my code:

dispatch({
      type: 'REQUEST_DATA',
      payload: {
        cardNo: accId,
        types: dataTypes,
        accOwner: accountOwner,
      },
    });

Expected is: if accountOwner is empty, then accOwner param should not be passed as payload, same as the other params

Euph
  • 339
  • 1
  • 4
  • 19
  • Empty as in empty string (`''`), `undefined` or `null`? – Janez Kuhar Apr 19 '21 at 20:18
  • Does this answer your question? [In Javascript, how to conditionally add a member to an object?](https://stackoverflow.com/questions/11704267/in-javascript-how-to-conditionally-add-a-member-to-an-object) – Janez Kuhar Apr 19 '21 at 20:20

1 Answers1

1

you could do something like this

let payload = {
  cardNo: accId,
  types: dataTypes,
}
if(accountOwner)payload.accOwner =accountOwner

dispatch({
      type: 'REQUEST_DATA',
      payload,
});
سعيد
  • 1,547
  • 1
  • 14
  • 32