在 JavaScript ES6 中使用 if-else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36396160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of if-else in JavaScript ES6
提问by Nesh
I am learning ES6 and following is my ES5 code which is running fine -
我正在学习 ES6,以下是我运行良好的 ES5 代码 -
var myArr = [34,45,67,34,2,67,1,5,90];
var evenArr = [];
var oddArr = [];
myArr.map(function(x){
if(x%2==0) evenArr.push(x);
else oddArr.push(x);
});
Now if I am converting this to ES6 I am getting errors of Unexpected token
near if
, let me know what I am doing wrong here -
现在,如果我将其转换为 ES6,我会收到Unexpected token
接近的错误if
,让我知道我在这里做错了什么 -
My ES6code -
我的ES6代码 -
var myArr = [34,45,67,34,2,67,1,5,90];
var evenArr = [];
var oddArr = [];
myArr.map( x => if(x%2==0) evenArr.push(x) else oddArr.push(x) )
回答by Lewis
You should use ternary operator for inline conditions.
您应该对内联条件使用三元运算符。
myArr.map( x => x%2==0 ? evenArr.push(x) : oddArr.push(x) )
回答by zerkms
That's because the arrow functions accept expressionswhile you're passing a statement.
那是因为箭头函数在您传递语句时接受表达式。
Your code is misleading: Array.prototype.map
implies you would use the result somehow, while you are not.
您的代码具有误导性:Array.prototype.map
暗示您会以某种方式使用结果,而您不会。
If you wanted to improve the semanticsof your code you would use Array.prototype.forEach
that is designed specifically to iterate over an array and not return anything:
如果您想改进代码的语义,您可以使用Array.prototype.forEach
专门设计用于迭代数组而不返回任何内容的代码:
var myArr = [34,45,67,34,2,67,1,5,90];
var evenArr = [];
var oddArr = [];
myArr.forEach(x => {
if (x % 2 === 0) {
evenArr.push(x);
} else {
oddArr.push(x);
}
});
References:
参考:
回答by thefourtheye
If you don't use brackets to define the body of Arrow functions, the body should be an expression. In your case, it is not expression, but an if
statement.
如果不使用方括号定义 Arrow 函数的主体,则主体应该是表达式。在你的情况下,它不是表达,而是一个if
陈述。
You need to define it with the compound body, like this
你需要用复合体来定义它,像这样
myArr.map(x => {
if (x % 2 === 0)
evenArr.push(x);
else
oddArr.push(x);
})
Or you should define it to return an expression, like this
或者你应该定义它来返回一个表达式,像这样
myArr.map(x => x%2==0 ? evenArr.push(x) : oddArr.push(x))
Note:You should not use map
to do this. map
should be used only when you need to create a new array from the values of another array. You should use forEach
, when you are dealing with functions which have side-effects. In your case, you are modifying objects which are outside the scope of your function. So forEach
would be the best fit here.
注意:您不应该使用它map
来执行此操作。map
仅当您需要从另一个数组的值创建新数组时才应使用。forEach
当您处理具有副作用的函数时,您应该使用, 。在您的情况下,您正在修改函数范围之外的对象。所以forEach
这里最适合。
回答by RobG
If an arrow function body contains more than one statement, it must be contained in a block. Also, you can't omit semicolons like that.
如果一个箭头函数体包含多个语句,它必须包含在一个块中。此外,您不能像这样省略分号。
forEachis more semantic (since your function just returns undefinedand you don't keep the new array anyway) and you can use the value of x%2
directly:
forEach更具语义化(因为您的函数只是返回undefined而您无论如何都不保留新数组)并且您可以x%2
直接使用的值:
myArr.forEach( x => {if(x%2) oddArr.push(x); else evenArr.push(x)} )
and there's also:
还有:
myArr.forEach(x => [evenArr,oddArr][x%2].push(x));
回答by Ashish Panzade
var myArr = [34,45,67,34,2,67,1,5,90];
var evenArr = [];
var oddArr = [];
myArr.map( x => (x%2==0)? evenArr.push(x) : oddArr.push(x) )
Try the code above. It seems to work.
试试上面的代码。它似乎工作。
I have just replaced if..else
with ternary operator(? :
).
我刚刚if..else
用三元运算符(? :
)替换了。