onlycode.org

javascript
jquery
html
angularjs
css
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
ternary operator alterna...

Ternary Operator alternatives for more than 2 values, just only code

react-native | javascript

edit
 buttonBorderColor={
   item.status === 0
     ? "#000000"
     : item.status === 1
       ? "#37CAFA"
       : "#FFFFFF" // if status is 2
}
const colors = ["#000000", "#37CAFA", "#FFFFFF"]
// ...
buttonBorderColor={ colors[item.status] }

28
CertainPerformance, 14.09.2018 09:42




edit
var buttonBorderColor,
  buttonBackgroundColor,
  buttonTextColor,
  buttonText

switch (item.code) {
  case 0:
    buttonBorderColor = '#000000'
    buttonBackgroundColor = null
    buttonTextColor = "#000000"
    buttonText = USER_STATUS.REQUESTED
    break;
  case 1:
    buttonBorderColor = '#37CAFA'
    buttonBackgroundColor = '#37CAFA'
    buttonTextColor = "#FFFFFF"
    buttonText = USER_STATUS.FOLLOWING
    break;
  case 2:
    buttonBorderColor = '#FFFFFF'
    buttonBackgroundColor = '#FFFFFF'
    buttonTextColor = "#FFFFFF"
    buttonText = USER_STATUS.ELSE
    break;
  default:
    break;
}

27
Geethu Jose, 14.09.2018 09:48




edit
buttonBorderColor={item.status === 0 ? "#000000" : (item.status === 1 ? "#000001" : "#37CAFA")}
if (item.status === 0) {
    buttonBorderColor = '#000000'
    buttonBackgroundColor = null
    buttonTextColor = "#000000"
    buttonText = USER_STATUS.REQUESTED
} else if (item.status === 1) {
    // do something
} else {
    // do something
}

26
Abinash Ghosh, 14.09.2018 09:43




edit
const pickValue = (status, v1, v2, v3) => 
    status === 0
        ? v1
        : status === 1 
            ? v2
            : v3;

return (
    <PersonHandler
        profilePicture={item.user.profileImage ? { uri: item.user.profileImage } : DefaultUser}
        firstName={item.user.firstName}
        lastName={item.user.lastName}

        buttonBorderColor={pickValue(item.status, "#000000", "#37CAFA", null)}
        buttonBackgroundColor={pickValue(item.status, null, "#37CAFA", null)}
        buttonTextColor={pickValue(item.status, "#000000", "#FFFFFF", null)}
        buttonText={pickValue(item.status, USER_STATUS.REQUESTED, USER_STATUS.FOLLOWING, null)}

        submitting={unfollowIsInProgress && item._id === unfollowingPerson._id}
        onButtonPress={() => this.onUnfollowPress(item)}
    />
);

25
cezn, 14.09.2018 09:50

Add your just only code

your name:    



If you need full text click here

Some similar links Ternary Operator alternatives for more than 2 values ↓:
Operator precedence with Javascript Ternary operator

shorthand ternary operator

ternary operator example with functions

Using the ternary operator with string concatenation



This project is created for those who just need only code, wihout maaaaaaaaaaany words.