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

How to use for each loop with odd index in javascript

I’m trying using excel4node to convert my data into excel,

here is my code:

names = ['AA','BB','CC']

names.forEach((title, index) => {
    sheet.cell(1, index, 1, index + 1, true).string(title);

    index += 2;
});

the problem is i want to only use odd number as index in this loop.

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

expecting sheet.cell code in loop like this:

sheet.cell(1, "1", 1, "2", true).string(title[0])
sheet.cell(1, "3", 1, "4", true).string(title[1])
sheet.cell(1, "5", 1, "6", true).string(title[2])

so how do i skip the even index number in for each loop

I tried used index += 2 or in the beginning of the loop (title, index = 1) or use continue, but won’t work…

please help, thanks

>Solution :

I think you can do like this.

sheet.cell(1, 2 * index + 1, 1, 2 * index + 2, true).string(title);

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