1

I'm trying to reach the center of a nested array using recursion. This is part of a larger problem I'm trying to solve. I'm sure the solution is somewhat elementary. I've been learning JS/web dev and am stumped.

Here's my code:

var j = [[[["hey!"]]]];

function getNested(obj) {
for (var i = 0; i < obj.length; i++) {
    if (Array.isArray(obj[i])) {
        obj = obj[i];
        getNested(obj);
    } 
    return obj[i];
  } 
}

The function is supposed to return the 'hey!' string, but I can't seem to get it right.

8
  • Why is there a for loop if you'll only ever find the first innermost nested element and return it? How is your code recursive? Commented Sep 28, 2013 at 20:24
  • 1
    What if there is more than one element in any of the nested arrays? Commented Sep 28, 2013 at 20:25
  • 1
    I don't see a place where getNested calls itself, be it directly or through mutual recursion. Commented Sep 28, 2013 at 20:25
  • What is findType? If you're looking for recursion, switch out that line for return getNested(obj) Commented Sep 28, 2013 at 20:25
  • 1
    I can't see recursion in your code. getNested() should call itself somewhere. What if your array looks like that ["hey1",[[["hey!", "hey2"]]]] for example? What should be returned? Commented Sep 28, 2013 at 20:31

2 Answers 2

3

You are close, just switch out the findType(obj) for return getNested(obj[i]). This will give you a recursive function that drills down into the array until the item in the array is not another array. Also, the for-loop was unnecessary, given your example input.

This will only work if your nested arrays have exactly one element. Others have expressed this in the comments.

var j = [[[["hey!"]]]];

function getNested(obj) {
    if (Array.isArray(obj)) {
        return getNested(obj[0]);
    } 
    return obj;
}

var str = getNested(j);
console.log(str); // "hey!"

jsFiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

2

There is no need for recursion, a simple while loop will already do the trick

var j = [[[["hey!"]]]];

function getNested(obj) {
    while (Array.isArray(obj)) { obj = obj[0]; } 
    return obj;
}

var str = getNested(j);
console.log(str); // "hey!"

The recursive implementation has the nice property that it is purely functional though if you rewrite it like so:

var j = [[[["hey!"]]]];

function getNested(obj) {
    return Array.isArray(obj)? getNested(obj[0]): obj;
}

var str = getNested(j);
console.log(str); // "hey!"

Still its performance would be worse though.

3 Comments

It uses a loop instead of recursion. Thus it should perform significantly better for deeply nested arrays.
May be you shoud write There is NO need for recursion
The recursive solution has the only advantage that it could be written in a more compressed way like so: function getNested(obj) { return Array.isArray(obj)? getNested(obj[0]): obj; } Still its performance would be worse.

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.