te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Ternary Operator alternatives for more than 2 values - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ternary Operator alternatives for more than 2 values - Stack Overflow

programmeradmin5浏览0评论

In my react-native applications, i had written a code like this.

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

                buttonBorderColor={item.status === 0 ? "#000000" : "#37CAFA"}
                buttonBackgroundColor={item.status === 0 ? null : "#37CAFA"}
                buttonTextColor={item.status === 0 ? "#000000" : "#FFFFFF"}          
                buttonText={item.status === 0 ? USER_STATUS.REQUESTED : USER_STATUS.FOLLOWING}

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

Now I have more than 2 statuses to handle, so the ternary operator here cannot be used. What will be the best approach to handle a situation like this?

I have 3 statuses now. 0, 1 and 2. According to the status I have to handle the following conditions.

 buttonBorderColor={item.status === 0 ? "#000000" : "#37CAFA"}
                buttonBackgroundColor={item.status === 0 ? null : "#37CAFA"}
                buttonTextColor={item.status === 0 ? "#000000" : "#FFFFFF"}          
                buttonText={item.status === 0 ? USER_STATUS.REQUESTED : USER_STATUS.FOLLOWING}

In my react-native applications, i had written a code like this.

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

                buttonBorderColor={item.status === 0 ? "#000000" : "#37CAFA"}
                buttonBackgroundColor={item.status === 0 ? null : "#37CAFA"}
                buttonTextColor={item.status === 0 ? "#000000" : "#FFFFFF"}          
                buttonText={item.status === 0 ? USER_STATUS.REQUESTED : USER_STATUS.FOLLOWING}

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

Now I have more than 2 statuses to handle, so the ternary operator here cannot be used. What will be the best approach to handle a situation like this?

I have 3 statuses now. 0, 1 and 2. According to the status I have to handle the following conditions.

 buttonBorderColor={item.status === 0 ? "#000000" : "#37CAFA"}
                buttonBackgroundColor={item.status === 0 ? null : "#37CAFA"}
                buttonTextColor={item.status === 0 ? "#000000" : "#FFFFFF"}          
                buttonText={item.status === 0 ? USER_STATUS.REQUESTED : USER_STATUS.FOLLOWING}
Share Improve this question asked Sep 14, 2018 at 6:39 Shashika VirajhShashika Virajh 9,47720 gold badges64 silver badges112 bronze badges 4
  • You can use if else ladder or switch case for this purpose – Eklavya Commented Sep 14, 2018 at 6:40
  • 1 You know you can write item.status === 0 ? A : (item.status === 1 ? B : C) right? – Rafalon Commented Sep 14, 2018 at 6:41
  • 2 Nested ternaries are great. Read this stuff from Eric Elliot - medium./javascript-scene/… – Meet Zaveri Commented Sep 14, 2018 at 7:01
  • Move stuff out of the damn view – Regis Portalez Commented Sep 14, 2018 at 20:24
Add a ment  | 

4 Answers 4

Reset to default 13

Sure you can use the ternary operator still, you just have to use it twice, for example:

 buttonBorderColor={
   item.status === 0
     ? "#000000"
     : item.status === 1
       ? "#37CAFA"
       : "#FFFFFF" // if status is 2
}

That said, it's a bit unfortable to read - you might consider using an array indexed by status whose value is the color you want instead:

const colors = ["#000000", "#37CAFA", "#FFFFFF"]
// ...
buttonBorderColor={ colors[item.status] }

Use switch to handle three statuses. Nesting ternary operators is not a wise practice.

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;
}

You can use this way

buttonBorderColor={item.status === 0 ? "#000000" : (item.status === 1 ? "#000001" : "#37CAFA")}

or you can use if else ladder

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

You can do it like this:

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)}
    />
);
发布评论

评论列表(0)

  1. 暂无评论