I have got an array like below
Array={A,B
C,D
E,F.....}
Length of the array is 60 like A,B is taken as 1st line(1st line contains two elements) and C,D is taken as 2nd line(2nd line contains two elements) and so on till 60th line.
while iterating through a loop, I need to access the array elements like below with two different variables R and F. R represents all the elements A,C,E etc and F represents all the elements B,D,F etc...
when i=1
Array.R=A
Array.F=B
when i=2
Array.R=C
Array.F=D
when i=3
Array.R=E
Array.F=F
and so on till 60
Please help how can I get this.
1 Answer 1
You can use an old-fashioned for-loop and increase the index by 2 instead of 1, like this:
const input = ['A','B','C','D','E','F'];
for ( let n = 0; n < input.length; n += 2 ) {
const R = input[ n ];
const F = input[ n + 1 ];
console.log({R, F});
}
answered Jul 19, 2021 at 13:55
Sarah Groß
10.9k4 gold badges30 silver badges53 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
rpav
Thanks, that works...but I'm now facing issue like the array is returning A,BC,DE,FG,....in this format, since there is no seperation between B and C.. in the linux window A,B are shown in one line and C,D in the next line, but it seems that there has to be a seperation between them...how can I do this...
Sarah Groß
Which linux window? Are you in a node context?
rpav
this code is a part of a javascript which I was running on a server using MobaXterm. The output of JRExecAction function is split with delimiter | to get this array. May be I need to place a comma ',' between B and C , D and E and so on ...
lang-js