Search code examples
arraysreactjscomponentsmap-function

Array of objects to map


Could anybody tell me where i am going wrong? I have a sample code here where i am mapping over an array of objects and trying to render each item but for some reason it renders the same thing the amount of times of how many items I have. Take a look at my code and pls tell me how to solve this. What i am trying to achieve is to have the

Contact

as a title and then phone and email to be each rendered once with their corresponding item.

Hope you understand what i mean :) Thanks!


Solution

  • You were initializing objects in contact array wrongly.

    Here is how you should initialize:-

    this.state = {
          contact: [{ phone: "000000", email: "bla@bla.com" }]
        };