0

I am trying to iterate over some values of an Object using .map(). I need to escape a value. My code is like below

let modalrows = Object.keys(this.props.element).map((item,index) => ( if(item !== 'id') { //I am getting error in this line <tr key={index}> <td className="ui header data_type"> {item === 'name'? 'Name':null} </td> <td>{this.props.element[item]} <span>Edit</span></td> </tr> } )); 

1 Answer 1

3

You need a bracets to start a code block. Right now you try to use if in a return expression. Fixed with braces:

let modalrows = Object.keys(this.props.element).map((item, index) => { if (item !== "id") { return ( <tr key={index}> <td className="ui header data_type"> {item === "name" ? "Name" : null} </td> <td> {this.props.element[item]} <span>Edit</span> </td> </tr> ); } }); 

Alternatively, you could use filter to first filter out unnecessary elements:

let modalrows = Object.keys(this.props.element) .filter(item => item !== "id") .map((item, index) => ( <tr key={index}> <td className="ui header data_type">{item === "name" ? "Name" : null}</td> <td> {this.props.element[item]} <span>Edit</span> </td> </tr> )); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.