Javascript 如果在地图返回中如何使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28567549/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:00:20  来源:igfitidea点击:

How to use if within a map return?

javascriptreactjsreact-jsx

提问by Banshee

I need to generate diffrent reactJS code based on datamodel but I get

我需要基于数据模型生成不同的 reactJS 代码,但我得到

In file "~/Scripts/Grid.jsx": Parse Error: Line 13: Unexpected token if (at line 13 column 15) Line: 52 Column:3

在文件“~/Scripts/Grid.jsx”中:解析错误:第 13 行:意外标记 if(在第 13 行第 15 列)行:52 列:3

With this code

有了这个代码

var GridRow = React.createClass({
    render: function() {
        var row;

        row = this.props.cells.map(function(cell, i) {
            return (
                if(cell.URL != null && cell.URL.length > 0){
                    <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>        
                }
                else {
                    <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>
                }
            );
        }.bind(this));

        return (
            <tr>
                {row}
            </tr>
        );
    }
});

The render part seems to be really limited in how it can be used?

渲染部分的使用方式似乎真的很有限?

回答by nilgun

You put returnstatement inside ifclause like so:

您将return语句放入if子句中,如下所示:

    row = this.props.cells.map(function(cell, i) {

        if(cell.URL != null && cell.URL.length > 0){
            return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;        
        }
        else {
            return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;
        }

    }.bind(this));

回答by S.Kiers

You could also use a ternary(inline if/else) statement. It might look like this:

您还可以使用三元(内联 if/else)语句。它可能看起来像这样:

row = this.props.cells.map(function(cell, i) {
    return (cell.URL != null && cell.URL.length > 0) ? 
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>) :
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>)
}.bind(this));

or es6

或 es6

row = this.props.cells.map((cell, i) => (cell.URL != null && cell.URL.length > 0) ? 
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>) :
        (<td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>)
);

but, for readability, I would suggest nilgun's answer.

但是,为了可读性,我建议使用 nilgun 的答案。

Although I would remove the else statement, since it is redundant. You could also remove the curly brackets, this is a matter of preference.

虽然我会删除 else 语句,因为它是多余的。您也可以删除大括号,这是一个偏好问题。

row = this.props.cells.map(function(cell, i) {
    if(cell.URL != null && cell.URL.length > 0)
        return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;        
    return <td className={cell.Meta.HTMLClass} key={i}>{cell.Text}</td>;
}.bind(this));