32

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);
    – Rooster
    Feb 4, 2014 at 20:27
  • @Rooster—that would be the conditional operator, also called a ternary operator. :-)
    – RobG
    Feb 4, 2014 at 20:51
  • @RobG short hand if is a much better and easier to remember name ;-P
    – Rooster
    Feb 4, 2014 at 21:20
  • No problemwith that, but in a technical forum it's good to include the appropriate technical term. :-)
    – RobG
    Feb 4, 2014 at 23:12

7 Answers 7

67

No, however you can use the ternary operator:

var testBoolean = true;

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

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

var x = {
  y: (function(){
       if (true) return 'somevalue';
     }())
};
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"
}
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"
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);

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.

1
  • 2
    Should be a comment not an answer
    – FabricioG
    Feb 6, 2020 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.