lodash chaining groupBy
<p>I'm trying to figure out how to convert an array of vehicle objects, that are only unique by trim/year, to an array nested object properties. Originally I was looping through all of the properties to organize the vehicles into a hierarchical structure of arrays i.e., make[model[trim[year[]]]], but I think it would be faster to look up the vehicles by object properties i.e., make.model.trim.year. I'm a lodash noob so I'm not sure how to go about this.</p>

<p>The data returned is structured like this:</p>

<pre><code>[
  {
    id:1
    makeCode:"Make1"
    modelCode:"Modela"
    selected:true
    trimCode:"D"
    yearCode:"2014"
  },
  {
    id:2
    makeCode:"Make1"
    modelCode:"Modela"
    selected:true
    trimCode:"D"
    yearCode:"2015"
  },
  {
    id:3
    makeCode:"Make1"
    modelCode:"Modela"
    selected:true
    trimCode:"D"
    yearCode:"2016"
  },
  {
    id:4
    makeCode:"Make1"
    modelCode:"Modela"
    selected:true
    trimCode:"LX"
    yearCode:"2014"
  },
  {
    id:5
    makeCode:"Make1"
    modelCode:"Modela"
    selected:true
    trimCode:"LX"
    yearCode:"2015"
  },
  {
    id:6
    makeCode:"Make1"
    modelCode:"Modela"
    selected:true
    trimCode:"LX"
    yearCode:"2016"
  },
  {
    id:7
    makeCode:"Make2"
    modelCode:"Modelb"
    selected:true
    trimCode:"D"
    yearCode:"2014"
  },
  {
    id:8
    makeCode:"Make2"
    modelCode:"Modelb"
    selected:true
    trimCode:"D"
    yearCode:"2015"
  },
  {
    id:9
    makeCode:"Make2"
    modelCode:"Modelb"
    selected:true
    trimCode:"D"
    yearCode:"2016"
  }
]
</code></pre>

<p>This is working correctly to get the objects ordered by makeCode:</p>

<pre><code>vm.makeGroups = _.groupBy(vm.selectedVehcileTypes, function(v) { return v.makeCode});
</code></pre>

<p>but I would like to do this on every level, so I have something like:</p>

<pre><code>vm.makeGroups = _.groupBy(vm.selectedVehcileTypes, function(v) { return v.makeCode});
vm.modelGroups = _.groupBy(vm.makeGroups, function(v) { return v.modelCode});
vm.trimGroups = _.groupBy(vm.modelGroups, function(v) { return v.trimCode});
</code></pre>

<p>Basically I want to chain the grouping so that the end result looks like this:</p>

<pre><code>{
  Make1: {
    modela: {
      D: {
           '2012': false,
           '2013': false 
           '2014': true // selected
         } 
       }
     }
   }
}
</code></pre>

<p>Any help is appreciated, thanks.</p>