I have started learning react.js recently, I was having issue understanding the map() function in JavaScript. I am providing code snippets where I am trying to iterate over the "person" component, while I am trying to output the result on the console I just observed its iterating twice but the components are displayed only once in the browser[enter image description here][1]. Can anyone tell me why its iterating twice?
//this is the state
class App extends Component {
state = {
persons: [
{id:'aksjsdk',name:'ABC',age:'26'},
{id:'aksjajsd',name:'XYZ',age:'25'},
{id:'aksjadskjd',name:'MNO',age:'27'}
],
showAllPersons: false
}
........
if(this.state.showAllPersons){
persons = (
<div>
{
this.state.persons.map((person,index) => {
console.log(person);//printing twice
return <Person
name = {person.name}
age = {person.age}
click = {this.deletePersonHandler.bind(this,index)}
key ={person.id}/>
})
}
</div>)
}
....... //console output
Object { id: "aksjsdk", name: "ABC", age: "26" }
Object { id: "aksjajsd", name: "XYZ", age: "25" }
Object { id: "aksjadskjd", name: "MNO", age: "27" }
Object { id: "aksjsdk", name: "ABC", age: "26" }
Object { id: "aksjajsd", name: "XYZ", age: "25" }
Object { id: "aksjadskjd", name: "MNO", age: "27" }