0

In the following code i am adding events in ev[] array by using ev.push() function like

ev.push({title: v.Message,start:v.StartDate,end:v.EndDate,id: v.EventsCalendarID}) 

and now i want to add a condition in which i want to check Isapproved==true or false and if its true i can add textColor: 'black' in ev.push() or textColor: 'red' if Isapproved== false. How can i perform this check?

var events = [
      {
        "EventsCalendarID": 21,
        "AdminUserName": "ManjitSingh",
        "StartDate": "2014-08-27 12:00 AM",
        "EndDate": "8/28/2014 12:00:00 AM",
        "Timezone": "Europe/London",
        "Message": "mabkja",
        "IsApproved": false,
        "UserID": "70348398-9b8e-48a2-bbfc-c2474146d5d5",
        "User": null
      },
      {
        "EventsCalendarID": 22,
        "AdminUserName": "ManjitSingh",
        "StartDate": "2014-08-29 12:00 AM",
        "EndDate": "8/29/2014 1:00:00 AM",
        "Timezone": "Europe/London",
        "Message": "ffd",
        "IsApproved": true,
        "UserID": "70348398-9b8e-48a2-bbfc-c2474146d5d5",
        "User": null
      }
    ];

function success(events) {
  var ev = [];
  $.each(events, function (i, v) {
    ev.push({
      title: v.Message,
      start:v.StartDate,
      end:v.EndDate,
      id: v.EventsCalendarID
    });
  });
}
1
  • 1
    Add textColor: (v.isApproved?'black':'red') Commented Aug 29, 2014 at 9:07

4 Answers 4

1

Please try with the below code snippet.

function success(events) {
  var ev = [];
  $.each(events, function (i, v) {
    ev.push({
      title: v.Message,
      start:v.StartDate,
      end:v.EndDate,
      id: v.EventsCalendarID,
      textColor: v.IsApproved ? 'black' : 'red'
    });
  });
}

Note If above condition is not worked than please try with the below

 textColor: v.IsApproved == true ? 'black' : 'red'
 textColor: v.IsApproved === true ? 'black' : 'red'
 textColor: v.IsApproved == 'true' ? 'black' : 'red'

Let me know if i am not understand your question.

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

Comments

0
ev.push({textColor: (Isapproved?'black':'red')})

Comments

0

You can use ternary operator ? :

ev.push({
    title:     v.Message,
    start:     v.StartDate,
    end:       v.EndDate,
    id:        v.EventsCalendarID,
    textColor: v.IsApproved === true ? 'black' : 'red'
});

1 Comment

There is no need in comparing v.IsApproved to true.
0

Just for completeness of the answers, you could of course do something like this too:

$.each(events, function (i, v) {
    var color;
    if (v.IsApproved) {
        color = 'black';
    else {
        color = 'red';
    }
    ev.push({
        title: v.Message,
        start: v.StartDate,
        end: v.EndDate,
        id: v.EventsCalendarID,
        textColor: color
    });
});

But it's definitely better to go with the ternary operation using ? :. Though in some cases, if the condition using this ternary operation would be too complicated, it might be better to go with the solution I stated above just for better code readability.

Comments

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.