35

Setting attributeTwo using an if statement. What is the correct way to do this?

var testBoolean = true;

var object = {
  attributeOne: "attributeOne",
  attributeTwo: if (testBoolean) { "attributeTwo" } else { "attributeTwoToo" },
}
4
  • 1
    use short hand if: (attributeTwo: (true ? some_value : some_other_value); Commented Feb 4, 2014 at 20:27
  • 1
    @Rooster—that would be the conditional operator, also called a ternary operator. :-) Commented Feb 4, 2014 at 20:51
  • @RobG short hand if is a much better and easier to remember name ;-P Commented Feb 4, 2014 at 21:20
  • 1
    No problemwith that, but in a technical forum it's good to include the appropriate technical term. :-) Commented Feb 4, 2014 at 23:12

8 Answers 8

69

No, however you can use the ternary operator:

var testBoolean = true;

var object = {
  attributeOne: "attributeOne",
  attributeTwo: testBoolean ? "attributeTwo" : "attributeTwoToo"
}
Sign up to request clarification or add additional context in comments.

Comments

15

You can use an if statement, if it is within a immediately invoked function.

var x = {
  y: (function(){
       if (true) return 'somevalue';
     }())
};

Comments

6

You can't use an if statement directly, but you can use ternary operator (aka conditional operator) which behaves the way you want. Here is how it would look:

var testBoolean = true;

var object = {
  attributeOne: "attributeOne",
  attributeTwo: testBoolean ? "attributeTwo" : "attributeTwoToo"
}

Comments

4

you can also do by this method

var testBoolean = true;

var object = {
  attributeOne: "attributeOne"
}

1

if(testBoolean){
   object.attributeTwo = "attributeTwo"
}else{
   object.attributeTwo = "attributeTwoToo"
}

2

object.attributeTwo = testBoolean ? "attributeTwo" : "attributeTwoToo"

Comments

3

you can put a conditional statement like this way, where conditional statement also works for object's key also.

let inbox = true;
let assign = {...(inbox ? {assign_to: 2} : {})};
console.log("assign", assign);

inbox = false;
assign = {...(inbox ? {assign_to: 2} : {})};
console.log("assign", assign);

Comments

0

I know this question is really old but i haven't seen anyone give this answer so i'll just drop this here, you can actually pass a function inside the property of your object like this:

var testBoolean = true;

const genericFunc = (testBoolean) => {
  if(testBoolean !== true){
    return 'False'
  }else {
    return 'True'
  } 
}

var object = {
  attributeOne: "attributeOne",
  attributeTwo: genericFunc(testBoolean),
}

console.log('Object',object)

As long as the function you pass actually has a return statement, it'll work!

Comments

0

To add a value conditionally in an object you can do something like this

const condition = true;
var obj = {
    key: condition ? value1 : value2
}

Comments

-4

Indeed you can but why don't you do the conditional statement before assigning it to object attribute. The code would be nicer.

1 Comment

Should be a comment not an answer

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.