Loop through multilevel objects & process all data

This needed to be tested so it could be used in production code. MORE looping rule = when the value (not the key) of the data sent is an object PROCESS rule = when the value (of the associated key) is NOT an object (and non-empty) then build an object to process (send to a processing function/method)

by Matt Rummler

HTML

<body>
    <form id='recordForm'>
      <fieldset id="Record">
            <legend id='recordLegend'>
              Record
              <!--<a href="/applicationform.html#recordForm">Record</a>-->
            </legend>           
        <input id="formName" type="hidden" name="formName" value="Record">
          <ul class="unmarkedList">
              <li id="recordNumberData">
                  <label for="Record_ID" data-inline="true">
                      Record Number: 
                  </label>
                <input id="Record_ID" name="Record_ID" type="text" readonly>
              </li>
              <li id="recordNameData">
                <label id="recordNameDataLabel" for="Record_Name">
                      Record Name: 
                  </label>                            
                <input id="Record_Name" type="text" name="Record_Name" value="" autofocus><!--value="[[+Record_Name]]""[[*Record_Name]]"/>-->
              </li>
              <li id="recordDescData">
                  <label id="recordDescDataLabel" hidden>
                      Record Description: 
                  </label>
                <!--use this to apply some default bootstrap styling class="form-control"-->
                <br/><textarea rows="3" id="Record_Description" name="Record_Description" contenteditable="true" hidden></textarea>
                            <!--                            <kbd id="Record_Description" name="Record_Description"></kbd>-->
              </li>
              <li>
                <label for="RecordType_ID" data-inline="true">
                  Record Type
                </label>
                <select id="RecordType_ID" name="RecordType_Name" form="recordForm"><!--form= allows the select to appear anywhere in the document not just in the <form></form>-->
                  <option selected="selected">

                  </option>
                 <option value="1">Complaint</option><option value="2">File</option><option...

JavaScript

Object.forEach=(function forEach()
{
  return function(obj,fn)
  {
    if(obj.length)
    {
      for (var i = 0, ol = obj.length, v = obj[0]; i < ol && fn(v, i) !== false; v = obj[++i]);
    }
	else
    {
      for (var p in obj) if (fn(obj[p], p) === false) break;
    }
  };
})(this);
Object.toType = (function toType(global)
{
  return function(obj) 
  {
    if (obj === global) 
    {
      return "global";
    }
    return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
  };
})(this);

function findDataToProcess(data)//,i)
{
    alert('@ the beginnnig of findDataToProcess');
  var keyTest=null;
  var previousKey=0;
//  i=i || 0;
  var localContext=this;
  var ProcessObject={};
  //ProcessObject.previousKey={};
  var allKeys=[];
  loopingFunction(data);
  function loopingFunction(data)
  {
//      alert('in the function loopingFunction');
    Object.forEach(data, function(value, key)
    {
//        alert('in loopingFunctions loop');
      if(Object.toType(value)=='object' || Object.toType(value)=='array')
      {
//          alert('value is an object or array & the current key is: '+key);
        previousKey=key;
          allKeys.push(key);
//        localContext.loopingFunction(value);
        loopingFunction(value);
      }
      else if(value)
      {
//          alert('value is not an object or array but has a value & the current value is: '+value+' while the current key is: '+key+' also the value of previousKey is: '+previousKey);
        ProcessObject[previousKey]={};
        ProcessObject[previousKey][key]=value;
      }
    });
    if(Object.getOwnPropertyNames(ProcessObject).length>=1)//.previousKey)
    {
        printOutObject(ProcessObject[previousKey]);
 //     processData(ProcessObject[previousKey],previousKey,allKeys);
    }
  }
}
/*
***
*/
function printOutObject(Obj)
{
  Object.forEach(Obj, function(value, key)
  {
      if(Object.toType(value)=='object')
      {
        printOutObject(value)
      }
      alert('Obj has a...