title: javascript - search array of nested objects and return parent if value is found in child
body: <p>There is an array of objects (489 objects). An object has a key which has another object (child object). Number of child object is indefinite. </p>
  
  <p>I've created <a href="https://jsfiddle.net/7eL7t0d1/" rel="noreferrer">a fiddle</a> as well. Please find below search function:</p>
  
  <pre><code>function getObjects(obj, key, val) {
      var objects = [];
      for (var i in obj) {
          if (!obj.hasOwnProperty(i)) continue;
          if (typeof obj[i] == 'object') {
              parent = obj;
              objects = objects.concat(getObjects(obj[i], key, val));    
          } else {
              if(obj[i].toString().toLowerCase().indexOf(val.toString().toLowerCase()) &gt; -1) {
                  objects.push(obj);
              }
          } 
      }
      return objects;
  } 
  </code></pre>
  
  <p>Here I want to search for a search string in an array of object including nested object (<code>asset_info</code>) as well. The search without <code>getObjects</code> works fine with below function.</p>
  
  <pre><code>    this.data.filter(function(row){ 
          var flag; 
          for(var prop in (columns.length &gt; 0 ? columns : row)){ 
            flag = false; 
            flag = row[columns.length &gt; 0 ? columns[prop] : 
  prop].toString().toLowerCase().indexOf(searchString.toString().toLowerCase()) &gt; -1; 
            if(flag) 
            break; 
          } 
      return flag;
  </code></pre>
  
  <p>In above function I am not able to search within nested object (<code>asset_info</code>). And so I have been using <code>getObject</code> function which I found online.</p>
  
  <p>Now the issue is I am not able to search including <code>asset_info</code> but not able to return its parent. For example in fiddle, if I search for <code>emc</code>, search should return 2nd object from <code>data</code>.</p>
  
  <p><strong>Update:</strong>
  <a href="https://jsfiddle.net/7eL7t0d1/6/" rel="noreferrer">still not able to return parent of a child where search string has been found</a></p>
  
  <p>** Update 2 **</p>
  
  <pre><code>var data = [
  {
    'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11',
    'asset_count': 2,
    'pdg': 'Invalid',
    'user_area': 'Invalid',
    'deployment_number': 'Invalid',
    'spoc': 'invalid',
    'release': 'Invalid',
    'start_date': '2017-06-12 00:00:00',
    'end_date': '2017-06-16 00:00:00',
    'asset_info': [
      {
        'bams_id': 'BAMS-1001423507',
        'hostname': 'GTVOSS11',
        'status': 10,
        'site_location': 'IEAT01 Tipperary',
        'rack_number': 'VIRTUAL RACK',
        'rack_u_position': 0,
        'manufacturer': 'EMC',
        'model': 'VM',
  
      },
      {
        'bams_id': 'BAMS-1001368001',
        'hostname': 'None',
        'status': 10,
        'site_location': 'IEAT01 Tipperary',
        'rack_number': 'VIRTUAL RACK',
        'rack_u_position': 0,
        'manufacturer': 'HP',
        'model': 'HP BL460C GEN8',
  
      }
  ],
  'full_name': 'Invalid (invalid)',
  'email_address': 'Invalid'
  },
  {
    'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11',
    'asset_count': 2,
    'pdg': 'Invalid',
    'user_area': 'Invalid',
    'deployment_number': 'Invalid',
    'spoc': 'invalid',
    'release': 'Invalid',
    'start_date': '2017-06-12 00:00:00',
    'end_date': '2017-06-16 00:00:00',
    'asset_info': [
      {
        'bams_id': 'BAMS-1001423507',
        'hostname': 'GTVOSS11',
        'status': 10,
        'site_location': 'IEAT01 Tipperary',
        'rack_number': 'VIRTUAL RACK',
        'rack_u_position': 0,
        'manufacturer': 'EMC',
        'model': 'VM',
  
      }
  ],
  'full_name': 'Invalid (invalid)',
  'email_address': 'Invalid'
  }];
  </code></pre>
  
  <p>Here if I search for a string 'emc', it should return 2 objects. 1st object will have only one child as there is only one object has 'manufacturer' as 'emc'.</p>
  
  <p><strong>The output should be:</strong></p>
  
  <pre><code>[
  {
    'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11',
    'asset_count': 2,
    'pdg': 'Invalid',
    'user_area': 'Invalid',
    'deployment_number': 'Invalid',
    'spoc': 'invalid',
    'release': 'Invalid',
    'start_date': '2017-06-12 00:00:00',
    'end_date': '2017-06-16 00:00:00',
    'asset_info': [
      {
        'bams_id': 'BAMS-1001423507',
        'hostname': 'GTVOSS11',
        'status': 10,
        'site_location': 'IEAT01 Tipperary',
        'rack_number': 'VIRTUAL RACK',
        'rack_u_position': 0,
        'manufacturer': 'EMC',
        'model': 'VM',
  
      }
  ],
  'full_name': 'Invalid (invalid)',
  'email_address': 'Invalid'
  },
  {
    'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11',
    'asset_count': 2,
    'pdg': 'Invalid',
    'user_area': 'Invalid',
    'deployment_number': 'Invalid',
    'spoc': 'invalid',
    'release': 'Invalid',
    'start_date': '2017-06-12 00:00:00',
    'end_date': '2017-06-16 00:00:00',
    'asset_info': [
      {
        'bams_id': 'BAMS-1001423507',
        'hostname': 'GTVOSS11',
        'status': 10,
        'site_location': 'IEAT01 Tipperary',
        'rack_number': 'VIRTUAL RACK',
        'rack_u_position': 0,
        'manufacturer': 'EMC',
        'model': 'VM',
  
      }
  ],
  'full_name': 'Invalid (invalid)',
  'email_address': 'Invalid'
  }];
  </code></pre>
  
  <p><strong>How do I search for a string within an array of objects including nested object and return parent object if search string is found in child object ?</strong></p>