Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Grouping nested groups using lodash

I've got a object a bit like this:

{
   "FORD": [
     {
       "ManufacturerName": "FORD",
       "Model": "Focus",
       "Price": "12345"
     },
     {
       "ManufacturerName": "FORD",
       "Model": "KA",
       "Price": "11111"
     },
     {
      "ManufacturerName": "FORD",
      "Model": "Focus",
      "Price": "22222"
     }
   ],
   "HONDA": [
    {
        "ManufacturerName": "HONDA",
        "Model": "JAZZ",
        "Price": "98765"
      },
      {
        "ManufacturerName": "HONDA",
        "Model": "JAZZ",
        "Price": "33333"
      }
   ]
}

I've got this far using lodash code like this:

    let myTest = _.chain(obj)
       .sortBy('ManufacturerName')
       .groupBy('ManufacturerName')

What I'm trying to do also also group it by Model, so result will look something like this:

   {
      "FORD": [
        {
          "Focus": [
            {
              "ManufacturerName": "FORD",
              "Model": "Focus",
              "Price": "12345"
            },
            { 
              "ManufacturerName": "FORD",
              "Model": "Focus",
              "Price": "12345" }
          ],
          "KA": [
             {
               "ManufacturerName": "FORD",
               "Model": "KA",
               "Price": "11111"
            }
          ]
        }
      ],
      "HONDA": [
          {
             "JAZZ": [
                { 
                  "ManufacturerName": "HONDA", 
                  "Model": "JAZZ", 
                  "Price": "33333" },
               { 
                  "ManufacturerName": "HONDA", 
                  "Model": "JAZZ", 
                  "Price": "98765" }
             ]
         }
      ]
    }

Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Map the object with _.mapValues() and group each each array (v) by the Model:

const obj = {"FORD":[{"ManufacturerName":"FORD","Model":"Focus","Price":"12345"},{"ManufacturerName":"FORD","Model":"KA","Price":"11111"},{"ManufacturerName":"FORD","Model":"Focus","Price":"22222"}],"HONDA":[{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"98765"},{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"33333"}]}

const result = _.mapValues(obj, v => _.groupBy(v, 'Model'))

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Or using a chain:

const obj = {"FORD":[{"ManufacturerName":"FORD","Model":"Focus","Price":"12345"},{"ManufacturerName":"FORD","Model":"KA","Price":"11111"},{"ManufacturerName":"FORD","Model":"Focus","Price":"22222"}],"HONDA":[{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"98765"},{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"33333"}]}

const result = _(obj)
  .mapValues(v => _.groupBy(v, 'Model'))
  .value()

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!