Javascript嵌套三元运算符

Javascript嵌套三元运算符

本文介绍了Javascript嵌套三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以向我解释,我会丢失一些东西。我正在尝试将现有代码重新编写为三元运算符。
我收到以下控制台错误:

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 :

未捕获的语法错误:意外的令牌}

我知道有一个条件格式不正确,我似乎找不到。因此,我不确定自己缺少什么,还是可能误解了过滤功能中的某些内容?不是吗? item.verified === true是否不应该自动返回正确的对象?

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

我了解的语法:

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/

推荐答案

是。您的语法不正确。要了解为什么代码无法正常工作,如果您稍微重新编写 if-else 语句,将会有所帮助。

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

为此:

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 )

其中 val2 => cond2吗? val3:(val4)

其中 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))

这里的括号很重要,因为它从上面模仿了嵌套的if-els。

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

还要注意,对于最里面的表达式,必须在else 中指定一个返回值。我将其设置为null,但您可以返回所需的内容。请注意,这是您的代码失败的实际原因。如果答案很长很抱歉,但我想帮助您了解嵌套三元运算符。

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.

这篇关于Javascript嵌套三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:50