Not understanding nested ternary operator

你说的曾经没有我的故事 提交于 2019-12-12 05:27:12

问题


I'm missing something if someone can please explain it to me. I'm trying to re-write existing code into the ternary operator way. I'm getting the following console error :

Uncaught SyntaxError: Unexpected token }

which I understand there's a condition not properly formatted which I can't seem to find. So I'm not sure what I'm missing or if I maybe misunderstand something in the filter function? Isn't the ? item.verified === true not suppose to automatically return the objects that's true?

var engagement = "social";
var audience = [{ 'verified': true, 'name': 'Steve'},
                { 'verified': false, 'name': 'Phil'},
                { 'verified': true, 'name': 'Jason'}];
let data = [];

data = audience.filter((item) => {
    (engagement === "social") 
    ? item.verified === true
    : (engagement === 'social-crm') 
    ? item.verified === false 
    : (engagement === 'all')
    ? item
})

The syntax that I understand:

data = audience.filter((item) => {
              if (this.engagement === 'social-crm') {
                return item.verified === true;
              } else if (this.engagement === 'social') {
                return item.verified === false;
              } else if (this.engagement === 'all') {
                return item;
              }
});

Here's the fiddle I've been trying to play around with: https://jsfiddle.net/phfilly/ya73e325/7/


回答1:


Yup. Your syntax isn't right. To understand why your code isn't working, it would help if you were to re-write your if-else statements a bit.

if (this.engagement === 'social-crm') {
    return item.verified === true;
} else if (this.engagement === 'social') {
    return item.verified === false;
} else if (this.engagement === 'all') {
    return item;
}

To this:

if(this.engagement === 'social-crm') { return item.verified === true; }
else {
   if(this.engagement === 'social') {item.verified === false; }
   else {
       if(this.engagement === 'all') {return item;} 
   }
}

Now, ternary operators follow a similar nested fashion.

cond1 ? val1 : ( val2 )

Where val2 => cond2 ? val3 : (val4)

Where val4 => cond3 ? val5 : val6

So, now you can rewrite your expression like this:

this.engagement === 'social-crm' ? item.verified === true : 
                             ( this.engagement === 'social' ? item.verified === false : 
                                                             (this.engagement === 'all' ?  item : null))

The parenthesis matters here, because it closely mimics the nested if-elses from above.

Also note that for the inner most expression, a return value in the else must be specified. I've set it to null but you can return what you want. Do note this is the actual reason your code was failing. Apologies if the answer was long but I wanted to help you understand nested ternary operators.




回答2:


A ternary operator looks like this:

something = (condition) ? a_value : a_different_value;

You forgot : a_different_value on the last case.




回答3:


Try this

You have to have a Condition for the Ternary operator as pointed by @Quentin

data = audience.filter((item) => { (engagement === "social") ? 
item.verified === true : (engagement === 'social-crm') ? 
item.verified === false : (engagement === 'all')? 
item : null})


来源:https://stackoverflow.com/questions/44716426/not-understanding-nested-ternary-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!