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

Is it bad practice to return on every iteration of a loop

I like how this map works but I have a suspition that it might be bad practice to use return on every iteration of a loop

Is this a bad practice?

Thank you

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

const array = [
  ["Header 1", "Header 2"],
  [1, 2],
  [3, 4],
];

const mappedArray = array.map((row, index) => {
  if (index === 0) {
    return row;
  } else {
    return row.map((value) => value * 2);
  }
});

I have tryed searching for an awnser to this but found nothing

>Solution :

This is fine, the way the array.map function works is by calling your function to see how to the map the values.

The main concern would be if you’re calling the variable often, if not I wouldn’t worry about it

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