0

I am practicing recursion and have written this function:

var bigArray = ['a', ['b', ['c', ['d', {a:'e'}]]]];
var results = [];

function bagger293(bigArray){
  for (var item in bigArray){
    if (Array.isArray(bigArray[item])){
      bagger293(bigArray[item]);
    }
     else if (typeof bigArray[item] === 'object'){
      bagger293(bigArray[item]);
    }else{
      results.push(bigArray[item]);
    }
  }
}

bagger293(bigArray);
console.log(results);

I simply want the function to push any single value to an array, and search deeper if it is an object or array. Oddly, my function returns this:

[ 'a', 'b', 'c', 'd', 'e', 'd', 'e', 'c', 'd', 'e', 'd', 'e', 'b', 'c', 'd', 'e', 'd', 'e', 'c', 'd', 'e', 'd', 'e' ]

I assume it is because of my way of specifying

bigArray[item] but don't know why exactly.

expected result:

['a', 'b', 'c', 'd', 'e']

1
  • I don't even see how this is any good in context of recursion. Commented Oct 20, 2015 at 14:39

3 Answers 3

4

You're missing an else. Once you've spotted an array, stop trying to do other things with it.

var bigArray = ['a', ['b', ['c', ['d', { a: 'e' }]]]];
var results = [];

function bagger293(bigArray) {
  for (var item in bigArray) {
    if (Array.isArray(bigArray[item])) {
      bagger293(bigArray[item]);
    } 
    else if (typeof bigArray[item] === 'object') {   // <--
      bagger293(bigArray[item]);
    } 
    else {
      results.push(bigArray[item]);
    }
  }
}

bagger293(bigArray);
console.log(results);

On the other hand, since all arrays are objects, you can just test for 'object':

var bigArray = ['a', ['b', ['c', ['d', { a: 'e' }]]]];
var results = [];

function bagger293(bigArray) {
  for (var item in bigArray) {
    if (typeof bigArray[item] === 'object') {
      bagger293(bigArray[item]);
    } 
    else {
      results.push(bigArray[item]);
    }
  }
}

bagger293(bigArray);
console.log(results);

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

1 Comment

I was slightly faster. :o
3

You are missing an else. Since an array is an object as well, your code starts the recursion twice.

This is working:

function bagger293(bigArray){
  for (var item in bigArray){
    if (Array.isArray(bigArray[item])){
      bagger293(bigArray[item]);
    }else if (typeof bigArray[item] === 'object'){
      bagger293(bigArray[item]);
    }else{
      results.push(bigArray[item]);
    }
  }
}

Comments

2

All arrays are objects. No need to check both Array.isArray and typeof == 'object'.

var bigArray = ['a', ['b', ['c', ['d', {a:'e'}]]]];
var results = [];
(function bagger293(bigArray) {
  for (var item in bigArray)
    if (typeof bigArray[item] === 'object')
      bagger293(bigArray[item]);
    else
      results.push(bigArray[item]);
})(bigArray);
console.log(results);

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.