title: Create JSON object by manipulating existing JSON object using Javascript
body: <p>I've a JSON object, in which I'm manipulating the data and trying to construct a new JSON object. The issue is I'm not able to find out the create a dynamic part of JSON object. Here is the actual JSON object</p>
  
  <pre><code>[{"acptFlag":true,"count":14288,"limsFlag":true,"plantId":30,"plantName":"Camilla, GA","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":false,"count":344,"limsFlag":true,"plantId":30,"plantName":"Camilla, GA","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":false,"count":700,"limsFlag":true,"plantId":31,"plantName":"Albany, KY","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":true,"count":9500,"limsFlag":true,"plantId":31,"plantName":"Albany, KY","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":false,"count":227,"limsFlag":true,"plantId":32,"plantName":"Green Forest, AR","supplierId":21,"supplierName":"Tyson Foods"},{"acptFlag":true,"count":7049,"limsFlag":true,"plantId":32,"plantName":"Green Forest, AR","supplierId":21,"supplierName":"Tyson Foods"},{"acptFlag":true,"count":10742,"limsFlag":true,"plantId":33,"plantName":"Dawson, GA","supplierId":21,"supplierName":"Tyson Foods"},{"acptFlag":false,"count":506,"limsFlag":true,"plantId":33,"plantName":"Dawson, GA","supplierId":21,"supplierName":"Tyson Foods"}]
  </code></pre>
  
  <p>The required JSON object is </p>
  
  <pre><code>[{"supplierName":"Keystone Foods","1":(344/(344+14288)) , "2":700/(700+9500))}},{"supplierName":"Tyson Foods"","1":(227/(227+7049)) , "2":(506/(506+10742))}}]
  </code></pre>
  
  <p>Here I'm trying to put "Keystone Foods" data in a single object. <code>"1":(344/(344+14288)) , "2":700/(700+9500))</code> represents the false count/ true count. "<code>1</code>" and "<code>2</code>" represent the count of Plant. I got stuck up in constructing the plant count code. </p>
  
  <p>Here is my existing code. </p>
  
  <pre><code>var resultJSON = {}, arr = [];
  var jsonObj = JSON.parse(obj)
  for (var key in jsonObj) {
    if (jsonObj.hasOwnProperty(key)) {
      var val = jsonObj[key];
      var supplierName = val.supplierName;
      arr.push(supplierName);    
    }
  }
  var M = {}, R = []
  getUniqueSuppliers(arr)
  function getUniqueSuppliers(arr) {
     for(var i = 0; i &lt; arr.length; i++) {
        M[arr[i]] = "supplierName"
     }
    for(var i in M) {
      R.push(i)
    }
  }
  checkEachSupplier(R)
  function checkEachSupplier(R) { 
      for (var index = 0; index &lt; R.length; index++) {
         var supplierName = R[index]
         createNewJSON(supplierName)
     }
  }
  getPlantTotalValueBasedOnSupplier(R);
  function createNewJSON(supplierName) {
  
      for (var key in jsonObj) {
         if (jsonObj.hasOwnProperty(key)) {
         var val = jsonObj[key];    
         if(supplierName == val.supplierName) {       
           resultJSON[supplierName] = {"supplierName" : supplierName}
         }
       }
     }
   }
   console.log(resultJSON)
  </code></pre>
  
  <p>My JSON fiddle :
  <a href="https://jsfiddle.net/7u5qphx9/6/" rel="nofollow">https://jsfiddle.net/7u5qphx9/6/</a></p>