0

I'm new in using jquery and i would like to ask what do you call this kind of statement where I want to add an If Statement inside

btw. the Read here is Boolean and i want to change if Read == true read: 'read' and if false it becomes unread: 'unread'

This is my Original code:

var options = { id: ID,
                read: Read == true ? 'unread' : 'read',
                category: "projects",
                message: Message
                };

and my Trial but didn't succeeded :

var options = { 
       if(Read == true) { 
                    id: ID,
                    read: 'read',
                    category: "projects",
                    message: Message
                        } 
        else {
                    id: ID,
                    unread: 'unread',
                    category: "projects",
                    message: Message
             }
    };

having expected identifier or string in the if word

Thanks in Advance

2
  • Tell us what is Read.. Add some more info.. Commented Aug 27, 2014 at 7:54
  • use ternary as opted by Claudix. Also, don't change property names for "read/unread", only the value (true/false i would choose myself). Makes your life so much easier to access it. Commented Aug 27, 2014 at 7:57

3 Answers 3

2

Use the ternary operator:

(expression_to_test) ? (value_if_true) : (value_if_false)

var options =  
    (Read == true) ? { 
                         id: ID,
                         read: 'read',
                         category: "projects",
                         message: Message
                    } 
                   : {
                        id: ID,
                        unread: 'unread',
                        category: "projects",
                        message: Message
                   }
;

NOTICE

The curly braces {} are NOT part of the ternary operator syntax. They are used here to create inline objects. In the code above, if Read == true then the operator returns the object created by the first curly braces.

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

6 Comments

btw sir I just want to ask what do you called this kind of sequence ? kinda new with this
The {} are intended to create an object. They are not for syntax purposes.
@EnriqueGil, this is called ternary operator and it's used in other languages such as C.
@Claudix - ah, yep see that now was looking at it like a normal if statement
Can i add an Alert statement inside the ternary operators?
|
0

If Read is a boolean then:

var Read = false;    
var options = { 
       id: 12,
       read: Read ? 'unread' : 'read',
       category: "projects",
       message: 'msg'
   };

console.log(options); // {id: 12, read: "read", category: "projects", message: "msg"} 

Read = true;
options = { 
       id: 12,
       read: Read ? 'unread' : 'read',
       category: "projects",
       message: 'msg'
   };

console.log(options); // {id: 12, read: "unread", category: "projects", message: "msg"} 

Fiddle

But it seems to me Read isn't a boolean, since if it was, then your original code would work:

var Read = false;    
var options = { 
       id: 12,
       read: Read == true ? 'unread' : 'read',
       category: "projects",
       message: 'msg'
   };

console.log(options); // {id: 12, read: "read", category: "projects", message: "msg"} 

Read = true;
options = { 
       id: 12,
       read: Read == true ? 'unread' : 'read',
       category: "projects",
       message: 'msg'
   };

console.log(options); // {id: 12, read: "unread", category: "projects", message: "msg"} 

Fiddle

Comments

0

Use the ternary operator like Claudix said, but;

Don't change the "structure of the options" if there is not structural difference. The difference is "is it read or not" => which should be represented by a single property on the options.

   var options = { 
                 id: ID,
                 read: Read,
                 category: "projects",
                 message: Message
              };

anywhere in your code you can now do a 'simple' if to check if it has been read.

   if (options.read) {
      // it was read
   } else {
      // it was not read yet.
   }

preventing the need of the "unread" property and "read/unread" string comparison.

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.