title: How to convert a nested javascript object to only first level properties object?
body: <p>I have the below object <code>inputObj</code> and I want to convert it to a simple object like <code>outputObj</code>.</p>
  
  <pre><code>var inputObj = {
      'a' : 1,
      'b' : true,
      'c' : 'string1',
      'd' : {
          'e' : 'string2',
          'f' : false,
          'g' : 5,
          'h' : {
              'i' : 7,
              'j' : 'string3',
              'k' : [
                  {
                      'name' : 'l',
                      'value': 11
                  },
                  {
                      'name' : 'm',
                      'value': {
                          'n' : 13,
                          'o' : 'string4'
                      }
                  }
              ]
          }
      },
      'p' : [
          {
              'name' : 'q',
              'value': 15
          },
          {
              'name' : 'r',
              'value': 'Awesome!'
          }
      ]
  }
  
  var outputObj = {
      'a' : 1,
      'b' : true,
      'c' : 'string1',
      'e' : 'string2',
      'f' : false,
      'g' : 5,
      'i' : 7,
      'j' : 'string3',
      'l' : 11,
      'n' : 13,
      'o' : 'string4',
      'q' : 15,
      'r' : 'Awesome!'
  }
  </code></pre>
  
  <p>Please note that in case of <code>array</code> the final output object would be build from <code>name</code> and <code>value</code> properties as per above example. </p>
  
  <p>I have tried to implement the same using below code. I know as I am calling the nested function, the final object is getting reset and the scope is also different there. May be basic closure concept can solve this but I am confused with this. If you can correct my code or altogether new code is also fine.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function convertToFirstLevelObject( object ) {
   var returnObj = {};
  
   if( IsPrimaryDataType( object )) {
    return object;
   }
   else {
    if( object instanceof Array ) {
           for ( var i = 0; i &lt; object.length; i++ ) {
            if( typeof object[i] === 'object' &amp;&amp; !(object[i] instanceof Array) ) {
             var key   = object[i]['name'],
              value = object[i]['value'];
             if( IsPrimaryDataType( value )) {
              returnObj[ key ] = value;
             }
             else {
              convertToFirstLevelObject( value );
             }
            }
            else{
             /* This condition is for Array of Array */
             if( object[i] instanceof Array ) {
              convertToFirstLevelObject( object[i] );
             } else {
              console.log('Wrong data passed, expected data id object or Array of objects');
              return;
             }
            }
           }
    }
    else {
     for ( var attr in object ) {
               if ( object.hasOwnProperty( attr ) ) {
                if( IsPrimaryDataType( object[ attr ] )) {
                 returnObj[ attr ] = object[ attr ];
                }
                else {
                 convertToFirstLevelObject( object[ attr ] )
                }
               } 
           }
    }
   }
   return returnObj;
  }
  
  function IsPrimaryDataType( input ) {
   var returnFlag = false;
   if( input === null || input === 'undefined' || typeof input !==  'object' ) {
    returnFlag = true;
   }
   return returnFlag;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Edit:</strong>
  Here is another <code>inputObj</code> just to show that nesting can be of any level, here I have increased the level of array nesting. In any level of array nesting it will just look for if there is any object which has <code>name</code> and <code>value</code> both the property then it will flatten that.</p>
  
  <pre><code> var inputObj = {
      'a' : 1,
      'b' : true,
      'c' : 'string1',
      'd' : {
          'e' : 'string2',
          'f' : false,
          'g' : 5,
          'h' : {
              'i' : 7,
              'j' : 'string3',
              'k' : [
                  {
                      'name' : 'l',
                      'value': 11
                  },
                  {
                      'name' : 'm',
                      'value': [{'n' : 13},{'o' : 'string4'}]
                  }
              ]
          }
      },
      'p' : [
          {
              'name' : 'q',
              'value': 15
          },
          {
              'name' : 'r',
              'value': 'Awesome!'
          }
      ],
      's' : [
          [{
              'name' : 't',
              'value': 17
          },
          {
              'name' : 'u',
              'value': 'string5'
          }],
          [ 1, 2, 3],
          [ "string6", "string7", "string8"],
          [
              [1,3,5],
              [{'name' : 'v', 'value' : 19, 'anyOtherProp' : false}],
              [2,4,6],
              [{'name' : 'w', 'otherProp' : 31}]
          ]
      ]
  }
  </code></pre>
  
  <p>OutObj should be like below</p>
  
  <pre><code>var outputObj = {
      'a' : 1,
      'b' : true,
      'c' : 'string1',
      'e' : 'string2',
      'f' : false,
      'g' : 5,
      'i' : 7,
      'j' : 'string3',
      'l' : 11,
      'n' : 13,
      'o' : 'string4',
      'q' : 15,
      'r' : 'Awesome!',
      't' : 17,
      'u' : 'string5',
      'v' : 19
  }
  </code></pre>