if else inside map function
Mistake in the tried solution
- data.attention is not an array.
- Check directly with value 1
Solution
- Using Array.map to return a copy of jsx elements.
- Using destructuring while looping through the array instead of writing item.attention
- Checking the value so basically we can use ternary check (true ? 1: 0), since the value of attention is either 1 or 0 which represents true or false we can use it in this way.
Notes to improve
- Instead of using wrapping with div you can use React fragments
const jsonData = { "data": [{ "id": 1, "attention": 1 }, { "id": 2, "attention": 1 }, { "id": 3, "attention": 0 } ]}
const App = () => { return ( <div> { jsonData.data.map(({attention}) => { return ( <div> { attention ? <span>✅</span> : <span>❌</span> } </div> ) }) } </div> ) }
ReactDOM.render(<App /> , document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script><div id='root'></div>
How to assign an IF statement inside a map function
ingredientIndex.forEach(currentIndex => {
^^^^^^^
Related Topics
Moment Js Get First and Last Day of Current Month
How to Save Binary Buffer to Png File in Nodejs
Regex to Allow Only Certain Special Characters and Restrict Underscore
How to Apply the Length Condition on the Existing Regular Expression
Prevent Swiping Between Web Pages in iPad Safari
How to Import File from Public Folder in React Application
Allowing/Replacing Comma as Number Separator for Input Type="Number" Using Rails
Javascript: Replace Last Occurrence of Text in a String
Javascript How to Create One Json by Combining Two Jsons
Check If a Number Has a Decimal Place/Is a Whole Number
How to Call Make Angular Service Synchronous
Wait for Http Request in Angular
How to Compare Two Json Have the Same Properties Without Order
Break the Loop of an Array Looping Function (Map, Foreach, etc.)
React Router - Stay At the Same Page After Refresh
Call Function Onpress React Native