title: How to combine array of objects into one object and then create another object within that array based on a condition on the index
body: <p>I have an array of objects that i need to flatten/simplify/combine based on certain conditions. Here is the shape of my current array of objects below:</p>
  <pre><code>const arrayOfObjects = 
    [ { Battery           : 'Battery'        } 
    , { batteryDetailsKey : [ 'Serial Number', 'Type', 'Part Number'       ] } 
    , { batteryDetailsVal : [ 'HJ3CA19347410218LJ98 100 QC', 'Extended Range', '4P94-Q001' ] } 
    , { Modules           : 'Modules'        } 
    , { moduleDetailsKey  : [ 'Serial Number', 'Part Number', 'Cell Count' ] } 
    , { moduleDetailsVal  : [ '83675327350061093222581609899001', 'LJ98-10C779-A01', '32'  ] } 
    , { assetSeparator    : 'assetSeparator' } 
    , { Battery           : 'Battery'        }  
    , { batteryDetailsKey : [ 'Serial Number', 'Type', 'Part Number'       ] } 
    , { batteryDetailsVal : [ 'HJ3CA19347410218LJ98 101 QC', 'Extended Range', '4P94-Q002' ] } 
    , { Modules           : 'Modules'        } 
    , { moduleDetailsKey  : [ 'Serial Number', 'Part Number', 'Cell Count' ] } 
    , { moduleDetailsVal  : [ '83675327350061093222581609899002', 'LJ98-10C779-A02', '28'  ] } 
    , { moduleDetailsVal  : [ '83675327350061093222581609899003', 'LJ98-10C779-A03', '27'  ] } 
    , { assetSeparator    : 'assetSeparator' } 
    , { Battery           : 'Battery'        } 
    , { batteryDetailsKey : [ 'Serial Number', 'Type', 'Part Number'                       ] } 
    , { batteryDetailsVal : [ 'HJ3CA19347410218LJ98 102 QC', 'Extended Range', '4P94-Q003' ] } 
    , { Modules           : 'Modules'        } 
    , { moduleDetailsKey  : [ 'Serial Number', 'Part Number', 'Cell Count' ] } 
    , { moduleDetailsVal  : [ '83675327350061093222581609899004', 'LJ98-10C779-A01', '32'  ] } 
    ] ]
  </code></pre>
  <p>I basically want this arrayOfObjects to be shaped into this structure:</p>
  <pre><code>const shapeIWantArrayOfObjects = 
    [ { Battery           : 'Battery'
      , batteryDetailsKey : [ 'Serial Number', 'Type', 'Part Number' ] 
      , batteryDetailsVal : [ 'HJ3CA19347410218LJ98 100 QC', 'Extended Range', '4P94-Q001' ] 
      , Modules           : 'Modules'
      , moduleDetailsKey  : [ 'Serial Number', 'Part Number', 'Cell Count' ] 
      , moduleDetailsVal  : [ '83675327350061093222581609899001', 'LJ98-10C779-A01', '32' ] 
      } 
    , { Battery           : 'Battery'
      , batteryDetailsKey : [ 'Serial Number', 'Type', 'Part Number' ] 
      , batteryDetailsVal : [ 'HJ3CA19347410218LJ98 101 QC', 'Extended Range', '4P94-Q002' ] 
      , Modules           : 'Modules'
      , moduleDetailsKey  : [ 'Serial Number', 'Part Number', 'Cell Count'] 
      , moduleDetailsVal  : [ '83675327350061093222581609899002', 'LJ98-10C779-A02', '28' ] 
      , moduleDetailsVal  : [ '83675327350061093222581609899003', 'LJ98-10C779-A03', '27' ] 
      } 
    , { Battery           : 'Battery'
      , batteryDetailsKey : [ 'Serial Number', 'Type', 'Part Number' ] 
      , batteryDetailsVal : [ 'HJ3CA19347410218LJ98 102 QC', 'Extended Range', '4P94-Q003' ] 
      , Modules           : 'Modules'
      , moduleDetailsKey  : [ 'Serial Number', 'Part Number', 'Cell Count'] 
      , moduleDetailsVal  : [ '83675327350061093222581609899004', 'LJ98-10C779-A01', '32' ] 
      } 
    ] 
  </code></pre>
  <p>As you can see i'm basically wanting to combine the modules and batteries details into one object, then as you can see i want to create another object within that array once i hit the <code>{&quot;assetSeparator&quot;: &quot;assetSeparator&quot;}</code>. That's like my conditional that tells me that asset has been combined, now time to combine the next one, almost think of it as <code>string.split(&quot;assetSeparator&quot;)</code></p>
  <p>Can someone please tell me how i could achieve this, i've tried <code>Object.assign({}, ...arrayOfObjects)</code> but that didn't quite achieve what i want, and i can't detect the <code>{&quot;assetSeparator&quot;: &quot;assetSeparator&quot;}</code> using the spread operator.</p>
  <p>I've also tried doing a reduce <code>arrayOfObjects.reduce(function(result, current) { return Object.assign(result, current); }, {})</code> but because it's accumulating an object, it's just override object properties with the same keys. Please help.</p>