Skip to main content
edited tags; edited title
Link
cнŝdk
  • 32.2k
  • 7
  • 62
  • 81

Javascript - Complex filter between 2 objects and a boolean

I have this array burgerActions :

[
  {
    statusId: 2,
    label: 'Accept'
  },
  {
    statusId: 3,
    label: 'Reject'
  },
  {
    label: 'Consult'
  },
  {
    label: 'Transfer'
  }
]

and this object status which can be either :

{
  "label": "Accept",
  "id": 2
}

or :

{
  "label": "Reject",
  "id": 3
}

In a first case you have to create a new array from burgerActions array where the statusId is not equivalent to the id of the status object that we have (we can only have one status object of the 2 cases that I listed). For that I used the filter method :

const result =  burgerActions.filter((element) => {
  return element.idstatusId !== recommendation.status.id
})

In the second case I need a complex conditioning adding with boolean

const statusRef = recommendation.recipient

In a first case I want if statusRef is false filter the last object in the burgerAction ({label: 'Transfer'}) but not in the case if statusRef is true. I need a method that does this type of filter without using the if () conditions because there are several cases to handle.

I have this array burgerActions :

[
  {
    statusId: 2,
    label: 'Accept'
  },
  {
    statusId: 3,
    label: 'Reject'
  },
  {
    label: 'Consult'
  },
  {
    label: 'Transfer'
  }
]

and this object status which can be either :

{
  "label": "Accept",
  "id": 2
}

or :

{
  "label": "Reject",
  "id": 3
}

In a first case you have to create a new array from burgerActions array where the statusId is not equivalent to the id of the status object that we have (we can only have one status object of the 2 cases that I listed). For that I used the filter method :

const result =  burgerActions.filter((element) => {
  return element.id !== recommendation.status.id
})

In the second case I need a complex conditioning adding with boolean

const statusRef = recommendation.recipient

In a first case I want if statusRef is false filter the last object in the burgerAction ({label: 'Transfer'}) but not in the case if statusRef is true. I need a method that does this type of filter without using the if () conditions because there are several cases to handle.

I have this array burgerActions :

[
  {
    statusId: 2,
    label: 'Accept'
  },
  {
    statusId: 3,
    label: 'Reject'
  },
  {
    label: 'Consult'
  },
  {
    label: 'Transfer'
  }
]

and this object status which can be either :

{
  "label": "Accept",
  "id": 2
}

or :

{
  "label": "Reject",
  "id": 3
}

In a first case you have to create a new array from burgerActions array where the statusId is not equivalent to the id of the status object that we have (we can only have one status object of the 2 cases that I listed). For that I used the filter method :

const result =  burgerActions.filter((element) => {
  return element.statusId !== recommendation.status.id
})

In the second case I need a complex conditioning adding with boolean

const statusRef = recommendation.recipient

In a first case I want if statusRef is false filter the last object in the burgerAction ({label: 'Transfer'}) but not in the case if statusRef is true. I need a method that does this type of filter without using the if () conditions because there are several cases to handle.

Javascript - Complex filtrefilter between 2 objects and boolean

added 186 characters in body
Source Link
Mouad Ennaciri
  • 1.3k
  • 3
  • 15
  • 28
Loading
Source Link
Mouad Ennaciri
  • 1.3k
  • 3
  • 15
  • 28
Loading