Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use if within a map return?

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

In file "~/Scripts/Grid.jsx": Parse Error: Line 13: Unexpected token if (at line 13 column 15) Line: 52 Column: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?

like image 426
Banshee Avatar asked Feb 17 '15 17:02

Banshee


People also ask

Can we use if statement inside map?

One can have if inside a function. Even if a ternary also works, you shouldn't suggest to remove something that's technically perfectly valid. @Chris: that's a fair comment but the OP's error is based on a syntax error created by trying to combine both an if statement and the ternary operator incorrectly...

How do you use condition inside a map?

To use a condition inside map() in React:Call the map() method on an array. Use a ternary operator to check if the condition is truthy. The operator returns the value to the left of the colon if the condition is truthy, otherwise the value to the right is returned.

Does .map require a return?

map() method allows you to loop over every element in an array and modify or add to it and then return a different element to take that elements place. However . map() does not change the original array. It will always return a new array.

What is the return value of map?

Map Values() Method in Java With Examples The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Parameter: This method does not take any parameter. Return value: It returns a collection view of all the values present in the map.


2 Answers

You put return statement inside if clause like so:

    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)); 
like image 87
nilgun Avatar answered Sep 27 '22 00:09

nilgun


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

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

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.

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

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)); 
like image 45
S.Kiers Avatar answered Sep 25 '22 00:09

S.Kiers