0

I have a object:

child_el = {
          height: sub_height,
          top: sizes.top,
          color: if sizes.top == 1 ? 0 : 1,
          };

How I can correctly put condition in property color?

Jadasdas
  • 759
  • 4
  • 15
  • 35
  • 3
    Remove the `if` and RTFM! – Aluan Haddad Apr 20 '18 at 07:04
  • I dont know why people put a downvote to such question. Everybody is not an expert and thus on this platform. var sizes={top:1}; var child_el = { height: sub_height, top: sizes.top, color: (sizes.top == 1 ? 0 : 1), }; – Ashish Jain Apr 20 '18 at 07:08
  • 1
    @AshishJain I had downvoted but changed my mind. This really shouldn't need to be asked here though. It is a well documented syntax. – Aluan Haddad Apr 20 '18 at 07:10

2 Answers2

3

You just need to remove the if from color property.

Check the below code :

child_el = {
   height: sub_height,
   top: sizes.top,
   color: sizes.top === 1 ? 0 : 1
};

Reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Ravi Sachaniya
  • 1,611
  • 17
  • 19
  • 1
    == operator can create problems in this case, have a look here https://stackoverflow.com/questions/523643/difference-between-and-in-javascript – Mr.Bruno Apr 20 '18 at 07:14
2
child_el = {
   height: sub_height,
   top: sizes.top,
   color: sizes.top === 1 ? 0 : 1,
};
Mr.Bruno
  • 4,519
  • 2
  • 11
  • 24