0

Im curious on how to iterate through an array and append the results to a react component. Ive gotten the part of sorting through an array of objects and grabbing the accounts that I want. Now im unsure of how to go about appending them to html.

export default class SortedTeams extends React.Component {


  renderContent(content){
    var entries = [{fullname: 'foo', title: 'designer'}, {fullname: 
    'foo2', 
    title: 'designer'}, {fullname: 'foo3', title: 'developer'}]
    var nohit = [];

    for(var i = 0; i <= entries.length - 1; i++) {

      if(entries[i].title === 'designer'){
        console.log('foohit');
  }
      else{
        nohit.push('nope')
   }
  }

  render() {
    var content = this.props.block.content;


    return(
      <SiteBuilderBlock tag="section" className="sortedTeams" block={this.props.block}>
        <div>
        <SiteBuilderList tag="div" list="teams" inGroupsOf={4} inGroupsOfClassName="row">
         {(element, key) => this.renderContent(element, key)}
      </SiteBuilderList>
    </div>

  </SiteBuilderBlock>
);

}

}

1

1 Answer 1

1

Use .map() to iterate through an array. In this case contacts is an array of objects like entries. Here I'm using an ES6 syntax and JSX.

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

return(
  <div>
    <ul>
      { contacts.map(contact => (
          <li key={contact.id}>
            {contact}
          </li>
        )
      )}
    </ul>
  </div>
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.