2

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.

5
  • 1
    give a correct input and output? Commented Dec 6, 2017 at 9:51
  • 1
    please add the wanted results as well. Commented Dec 6, 2017 at 9:51
  • What do you mean by filter 2 objects and in addition a boolean? Commented Dec 6, 2017 at 9:51
  • I edited the post Commented Dec 6, 2017 at 10:09
  • @MouadEnnaciri I posted an answer, please check it and tell me if that's what you are looking for. Commented Dec 6, 2017 at 10:26

1 Answer 1

1

If you want to filter the objects that doesn't have the same statusId as in the recommendation.status.id and if recommendation.recipient === true filter the ones that doesn't have statusId, you can use this condition in the filter callback function:

element.statusId !== recommendation.status.id && recommendation.recipient ? element.statusId != null : true;

Where && recommendation.recipient ? element.statusId != null : true means that if recommendation.recipient is true filter elements with a statusId.

Here's how should be your code:

var recommendation = {
  status: {
    "label": "Pending",
    "id": 1
  },
  recipient: true
};

const result = burgerActions.filter((element) => {
  return element.statusId !== recommendation.status.id && recommendation.recipient ? element.statusId != null : true;
});

Demo:

var burgerActions = [{
    statusId: 2,
    label: 'Accept'
  },
  {
    statusId: 3,
    label: 'Reject'
  },
  {
    label: 'Consult'
  },
  {
    statusId: 1,
    label: 'Transfer'
  }
];

var recommendation = {
  status: {
    "label": "Pending",
    "id": 1
  },
  recipient: true
};

const result = burgerActions.filter((element) => {
  return element.statusId !== recommendation.status.id && recommendation.recipient ? element.statusId != null : true;
});


console.log(result);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.