Assume we need to display a list of users every time we construct a li >component. Instead, I use the map function to create the li > element many times. import React from 'react' const App = () => { const user = ['Rahul', 'Shivam', 'Ayesha', 'Akash', 'Poonam']; return { user.map((names,i) => { return{names} }) } } export default App; You may have observed that we did not generate li > elements 5 times. We iterated the array with a map, and the map returned the new element. The output will be as follows: Figure 2. using the map Method Looking for an esteemed Angular development company ? Connect us now. See here 2) For
Users can use the standard “for” loop for creating the li> element. Here you have to use the length function for giving the last point of the loop. import React from 'react' const App = () => { const users = [ { id: 1, Name: 'Rahul' }, { id: 2, Name: 'Shivam' }, { id: 3, Name: 'Ayesha' }, { id: 4, Name: 'Akash' }, { id: 5, Name: 'Poonam' } ]; const displayUser = (users) => { let name = []; for (let i = 0; i < users.length; i++) { name.push( {users[i].Name}) } return name } return {displayUser(users)} } export default App; 3) For-in
The For-in loop allows you to iterate through value and property. Let's look at an example of how to utilize a For-in loop. import React from 'react' const App = () => { const users = [ { id: 1, Name: 'Rahul' }, { id: 2, Name: 'Shivam' }, { id: 3, Name: 'Ayesha' }, { id: 4, Name: 'Akash' }, { id: 5, Name: 'Poonam' } ]; const displayUser = (users) => { let name = []; for (let idx in users) { const item = users[idx]; name.push({item.Name}) } return name } return {displayUser(users)} } export default App; Read More: React v18.0: A guide to its new features and updates 4) For-of
For-of loop lets you loop over iterable data structures such as arrays, maps, strings, etc. Let’s understand it with an example. import React from 'react' const App = () => { const users = [ { id: 1, Name: 'Rahul' }, { id: 2, Name: 'Shivam' }, { id: 3, Name: 'Ayesha' }, { id: 4, Name: 'Akash' }, { id: 5, Name: 'Poonam' } ]; const displayUser = (users) => { let name = []; for (let item of users) { name.push({item.Name}) } return name } return {displayUser(users)} } export default App; 5) Filter
When we have to apply a condition to records and then show the filtered record using a loop, we must use the filter and map methods both at the same time, which is known as the "chaining technique." The filter will yield a new array, and we will apply the map function to the new array. import React from 'react' const App = () => { const users = [ { id: 1, Name: 'Rahul' }, { id: 2, Name: 'Shivam' }, { id: 3, Name: 'Ayesha' }, { id: 4, Name: 'Akash' }, { id: 5, Name: 'Poonam' } ]; return { users .filter((user) => user.Name.includes('m')) .map((item) => { return{item.Name} }) } } export default App; While learning all these loops, you might have noticed a ‘key’ property used in the li> element. You're probably wondering why we utilized it here. What is its significance? Let’s discuss it. Importance of key in the loop The Key property in ReactJS is used to determine whether any elements have changed, which elements have been added, and which elements have been removed. If we do not use the key, you will get one warning in the console. Check out the image given below. Figure 3. key warning Normally, the index is used as a key. However, the use of the index as a key has a detrimental influence on the performance of a React App. React officially recommends using string as a key. Conclusion Loops are crucial subjects to master for every developer since they help them reduce a lot of repetitive work, especially when iterating through numerous data and data structures. In this blog, we learned what a loop is, the types of loops in React-JSX, and how each of them helps to reduce load realistically. We've also seen the significance of a key element and the consequences of not employing it.