31

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" },
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jorge Olivero
  • 3,705
  • 9
  • 27
  • 33
  • 1
    use short hand if: (attributeTwo: (true ? some_value : some_other_value); – Rooster Feb 04 '14 at 20:27
  • @Rooster—that would be the [conditional operator](http://ecma-international.org/ecma-262/5.1/#sec-11.12), also called a [ternary operator](http://en.wikipedia.org/wiki/%3F:). :-) – RobG Feb 04 '14 at 20:51
  • @RobG short hand if is a much better and easier to remember name ;-P – Rooster Feb 04 '14 at 21:20
  • No problemwith that, but in a technical forum it's good to include the appropriate technical term. :-) – RobG Feb 04 '14 at 23:12

7 Answers7

65

No, however you can use the ternary operator:

var testBoolean = true;

var object = {
  attributeOne: "attributeOne",
  attributeTwo: testBoolean ? "attributeTwo" : "attributeTwoToo"
}
Matt
  • 20,108
  • 1
  • 57
  • 70
13

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

var x = {
  y: (function(){
       if (true) return 'somevalue';
     }())
};
Tony Nardi
  • 413
  • 2
  • 7
5

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"
}
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
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"
bajran
  • 1,433
  • 14
  • 23
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);
Talha Noyon
  • 824
  • 7
  • 13
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!

-4

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

HieuHT
  • 459
  • 5
  • 13