Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Array.push updates itself while using inside map function

Here I have used two new arrays for catching URL and Filename value. When I view it on console I got different values everytime like array got overwritten from previous values, Am I doing anything wrong?

downLoadFilesAsZip = async(item) => {

for (const index in item.docLinks) {
  let urlArr=[];
  let fileNameArr = [];
  const url = item.docLinks[index];
  let linkParts = url.split("?");
  let filename = linkParts[0].substring(linkParts[0].lastIndexOf('/')+1);
  urlArr.push(url);
  fileNameArr.push(filename);  
}
}

downLoadAllAsZip = () => {
  {
    this.state.docGroups.length !== 0 && this.state.docGroups.map((item) => {
      if(item.docLinks !== null && item.docLinks.length > 0){
          return(
            this.downLoadFilesAsZip(item)
          );
      }
    })
  }  
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Follow the loop. Every time your loop runs you are setting empty arrays.

let urlArr=[];
let fileNameArr = [];

bring that outside of your for loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading