1

I have a recursive function to work with nested arrays that are multi-level deep. The code below is supposed to select random elements, one from each level, and combine them into an array (in the order they are printed out in the console). However, the resulting array only contains elements of the highest levels (A, B or C, D or C), nothing below that. What is the reason for that?

const arry = [
  ["A"],
  ["B", "C"],
  ["D", "E"],
  [
    [
      ["F1", "F2"],
      ["G1", "G2"],
      [
        "H1",
        "H2",
        [
          ["I1", "I2", "I3"],
          ["J1", "J2"],
          ["K1", "K2", "K3"],
        ],
      ],
    ],
  ],
];

function rndmElementSelection(array) {
  rndElm = array[Math.floor(Math.random() * array.length)];
  return rndElm;
}

function recursion(array, resultAry = []) {
  array.forEach((element) => {
    if (typeof element === "string") {
      console.log(element);
      resultAry.push(element);
    } else {
      nE = rndmElementSelection(element);
      if (typeof nE === "string") {
        console.log(nE);
        resultAry.push(nE);
      } else {
        recursion(nE);
      }
    }
  });
  return resultAry;
}

console.log(recursion(arry));
2
  • Even after trincot's fix, there is some unusual behavior, and I'm not sure there's a good way around it if your input structure is as described. Because the arrays of Is, Js, and Ks are at the same level as the H strings, you will get either an H (e.g. ["A", "C", "D", "F2", "G1", "H2"]) or one I, one J, and one K (e.g. ["A", "C", "E", "F2", "G1", "I3", "J1", "K2"]). Is this your intent? Commented Jun 17, 2022 at 13:01
  • 1
    Yes, that is intended. Basically one random option from a level, be it just one element or an array of elements. Commented Jun 25, 2022 at 14:46

3 Answers 3

2

The problem is that your recursive call does not pass the second argument.

Without passing it, each recursive call will just populate its own, new array. It does return that array to the caller, but the caller (making the recursive call) ignores that returned value, so all the work of the recursive call is for nothing.

So the easy fix is to change this:

} else {
    recursion(nE);

to this:

} else {
    recursion(nE, resultAry);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:


function recursion(array, resultAry = []) {
  array.forEach((element) => {
    if (typeof element === "string") {
      console.log(element);
      resultAry.push(element);
    } else {
      nE = rndmElementSelection(element);
      if (typeof nE === "string") {
        console.log(nE);
        resultAry.push(nE);
      } else {
        resultAry = [...resultAry, ...recursion(nE)];
      }
    }
  });
  return resultAry;
}

Comments

0

This is perhaps a cleaner approach:

const pickSome = (xs) =>
  Array .isArray (xs)
    ? xs .every (x => Array .isArray (x))
      ? xs .flatMap (pickSome)
      : pickSome (xs [~~ (Math .random () * xs .length)]) 
    : [xs]

const arry = [["A"], ["B", "C"], ["D", "E"], [[["F1", "F2"], ["G1", "G2"], ["H1", "H2", [["I1", "I2", "I3"], ["J1", "J2"], ["K1", "K2", "K3"]]]]]]

for (let n = 0; n < 10; n ++) console .log (`['${pickSome (arry) .join ("', '")}'']`)
.as-console-wrapper {max-height: 100% !important; top: 0}

We test if our input is an array. If it's not, we simply wrap it in an array and return it. So pickSome ('G1') return ['G1']. If it is an array, then we check whether all its children are arrays. If they are we return the result of recursively flatMapping our function over its elements. If they are not, then we randomly choose one element and call pickSome on that. (This handles your ['H1', 'H2', [['I1', 'I2'...], ...]] case.)

Note that ~~ acts as an integer truncation operator, an alternative to Math .floor for positive numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.