0
const numArr = [1,2,3,4];
for (let i=0; i<numArr.length; i++) {
let indexNum = String(numArr[i]);
console.log(indexNum+numArr[0],indexNum+numArr[1],indexNum+numArr[2],indexNum+numArr[3]);}

result is

  • 11 12 13 14
  • 21 22 23 24
  • 31 32 33 34
  • 41 42 43 44

I would like to get this output?

  • 12 13 14
  • 23 24 34
  • 11 22 33 44 How do i iterate array. I want to start second iteration is index-1 and third iteration start index-2, fourth is index-3 and so on..

How do I looping this array..

1 Answers1

0

You can try nested loop. Maybe this will help.

const numArr = [1, 2, 3, 4];
for (let i = 0; i < numArr.length; i++) {
    for (let j = i; j < numArr.length; j++) {
        console.log(numArr[i] + "" + numArr[j]);
    }
}

or try to use map to avoid the nested loops and it will give you all the results in one array

const numArr = [1, 2, 3, 4];
let output = numArr.map(x => numArr.map(y => x + "" + y));
console.log(output.flat());
delia
  • 126
  • 2