Many Unwanted "undefined"s


I have attempted to make a function that will raise each element in my nested array by one level. I think I have done it, however I am getting many returns of ‘undefined’. The pattern sequence of ‘undefined’s’ equates to how my nesting is arranged (3 UD, 3 UD, 5UD, 5UD, 5UD, 4UD), but I do not understand what operation even happened to create returns like this.

Thanks in advance :slight_smile:
Screen Shot 2022-09-29 at 15.49.40

const arr2 = [[11, 12, 13], [14, 15, 16], [17], [18], [19], [[110], [111, [112, 113]]]];

function flattenArrayByOne(arrayOfArrays) {
  const levelUp = [];
  for (let i = 0; i < arrayOfArrays.length; i++) {
    for (let j = 0; j < arrayOfArrays.length; j++) {

  return levelUp;

console.log( flattenArrayByOne(arr2) );

the problem is the condition in your second loop.
You should be looping the length of the inner array (not the outer array)

1 Like

Log out console.log(arrayOfArrays[i][j]); inside the loop. Are you sure your nested loop is correct?

Haha, sometimes it’s the obvious!


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.