Jose3d
Jose3d

Reputation: 9277

Json object condition

i would like to create a Json object using an AND condition (i dont know if its possible). I put the unfinished code that i've by the moment.

var parameters = {
                Title : "hello",
                Text : "world",
                Question : //if two checkboxes are checked then true else false.
}

About the two checkboxes checked i was thinking to use this:

$('.checkbox1').is(':checked') && $('.checkbox2').is(':checked')

What i would like to avoid is to put an if like

//if checkboxes are checked
//{
//create the json object of type 1
//}
//else
//{
//create the json object of type 2
//}

Is this possible?

Upvotes: 0

Views: 1984

Answers (1)

jensgram
jensgram

Reputation: 31508

Yes, a boolean would be ok:

var parameters = {
            Title : "hello",
            Text : "world",
            Question : $('.checkbox1').is(':checked') && $('.checkbox2').is(':checked')
}

This will create a regular JavaScript object which you can manipulate like any other object, e.g.,

parameters.Question = <newTrueOrFalseToSet>;

Or even:

parameters["Question"] = <newTrueOrFalseToSet>;

According to the JSON definition, the resulting JSON should look like:

{
    "Title": "hello",
    "Text": "world",
    "Question": true
}

Upvotes: 1

Related Questions