How to add an array within an array?

Asked

Viewed 117 times

1

I’m creating a Reactjs application that hits the Github API and brings the user and the repositories.

But I wanted to bring the commits from every repository and I’m not getting.

My code is this:

async handleSubmit(e) {
    e.preventDefault();

    let user = await this.getUser(this.refs.username.value);
    let repo = await this.getUserRepo(this.refs.username.value);


    for(let i = 0; i < repo.length;i++){
       let commitao = []
       var comitasso = []
       commitao = await this.getReposCommit(this.refs.username.value, repo[i].name);
       comitasso.push(commitao)
       console.log(commitao)

    }
    console.log(comitasso)
    this.setState({
      avatar_url: user.avatar_url,
      username: user.login,
      id: user.id,
      url: user.html_url,
      message: user.message,
      reponame: repo,

    });

The problem is that when I do console.log(comitasso), he brings me only the last record.

  • Ever tried to make comitasso.push(commitao[0])? Or give console.log(commit) before giving the push?

  • Generally, the this.getReposCommit() should bring an object or an object array, so I asked about giving the console.

  • can put an example of the data returning in "commitao"?

  • @Leandro, Do not change the question to thank/indicate that the problem has been solved, start a new question in the same post. The best way to thank and accepting the answer who helped you. For these reasons I reverse the edit. =D -- Take a look at our [Tour] to better understand the functioning of the community.

2 answers

1

The problem is that you are setting the variable comitasso within the for, every interaction it will reset, only the last interaction will not be reset and so she is the only one that appears on the console, to solve just set the variable outside the for as shown below:

var comitasso = [];      /// ; <- defina aqui por exemplo
for(let i = 0; i < repo.length;i++){
   let commitao = [];
   // var comitasso = [] /// ; <- comentado
   commitao = await this.getReposCommit(this.refs.username.value, repo[i].name);
   comitasso.push(commitao)
   console.log(commitao)

}
  • 1

    It worked perfectly! Incidentally, I understand the logic now, rs. Thank you very much.

1


An even more efficient way to do this loop would be to send the request to fetch all commits first, and then wait for their resolution simultaneously, rather than sending the request to fetch one commit at a time, and then wait for the resolution of each before seeking the next.

var promiseComitasso = repo.map(r => this.getReposCommit(this.refs.username.value, r.name))
var comitasso = await Promise.all(promiseComitasso)

There is no need for a for here because the iteration of the array is done in the map.

  • Also solved! was even less verbose. I liked it like this. The problem is that I don’t understand much of the files yet =( I’ll try to understand better! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.