- Jan 2, 2006
 
- 10,455
 
- 35
 
- 91
 
I always have a very very difficult time thinking through recursion. For me it's just so weird to be calling the function in the definition of the function. It's hard enough for me to follow an already written recursive function, much less write one myself. And everything just goes to hell if the function starts calling itself *multiple* times in its own function definition, such as in the case of Fibonacci.
Having a loop and looping straight through from the start condition to the end condition just makes more intuitive sense to me.
I've got an array of nested objects
	
	
	
		
I want to create a function such that:
	
	
	
		
====
	
	
	
		
returns:
	
	
	
		
=======
	
	
	
		
returns:
	
	
	
		
======
	
	
	
		
returns:
	
	
	
		
	
	
	
		
returns
	
	
	
		
It's easy enough to find values for arrays with non-nested objects or if you know the exact level that you want to search in. But I'm not sure how to go about just finding "any value, anywhere" in an array that can contain any kind of object. I quickly get into looping hell and people say that recursion is where I should be heading.
I can use Underscore if that helps.
			
			Having a loop and looping straight through from the start condition to the end condition just makes more intuitive sense to me.
I've got an array of nested objects
		Code:
	
	    var myArray = [
      {
        tires: 2,
        exterior: {
          color: 'white',
          length: 2,
          width: 1
        }
      },{
        tires: 4,
        exterior: {
          color: 'blue',
          length: 5,
          width: 3
        }
      },{
        tires: 4,
        exterior: {
          color: 'white',
          length: 2,
          width: 3
        }
      }
    ];
	I want to create a function such that:
		Code:
	
	    var findItems = function(arr, value){
      // return array of found items
    };
	
		Code:
	
	    findItems(myArray, 'white');
	returns:
		Code:
	
	    [{
      tires: 2,
      exterior: {
        color: 'white',
        length: 2,
        width: 1
      }
    },{
      tires: 4,
      exterior: {
        color: 'white',
        length: 5,
        width: 3
      }
    }]
	=======
		Code:
	
	    findItems(myArray, 2);
	returns:
		Code:
	
	    [{
      tires: 2,
      exterior: {
        color: 'white',
        length: 2,
        width: 1
      }
    },{
      tires: 4,
      exterior: {
        color: 'white',
        length: 2,
        width: 3
      }
    }]
	======
		Code:
	
	    findItems(myArray, { tires: 2 });
	returns:
		Code:
	
	    [{
      tires: 2,
      exterior: {
        color: 'white',
        length: 2,
        width: 1
      }
    }]
	
		Code:
	
	findItems(myArray, { width:1, color:'white' })
	returns
		Code:
	
	      [{
        tires: 2,
        exterior: {
          color: 'white',
          length: 2,
          width: 1
        }
      }]
	It's easy enough to find values for arrays with non-nested objects or if you know the exact level that you want to search in. But I'm not sure how to go about just finding "any value, anywhere" in an array that can contain any kind of object. I quickly get into looping hell and people say that recursion is where I should be heading.
I can use Underscore if that helps.
			
				Last edited: 
				
		
	
										
										
											
	
										
									
								
				
		
			
		