1

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.

jabaa
  • 5,844
  • 3
  • 9
  • 30
Strontium_99
  • 1,771
  • 6
  • 31
  • 52
  • Can you explain your code? It doesn't make much sense for me. `obj` is an object,isn't it? – jabaa Feb 14 '22 at 14:58
  • Yes. A Json object. – Strontium_99 Feb 14 '22 at 15:01
  • There is only one [JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) in JavaScript and your code would make even less sense for `obj === JSON`. I assume, you mean a JavaScript object. `sortBy` returns an array containing two arrays. Calling `groupBy` on it doesn't make much sense. – jabaa Feb 14 '22 at 15:02
  • Ok. I might not be using the correct terminology. The "obj" is json. The first block of code is where I'm up to following the _.chain. I'm just trying to work out how to group within a group. If you see what I mean. – Strontium_99 Feb 14 '22 at 15:08
  • JSON is a text format. _"The "obj" is json."_ means that `obj` is a string. `sortBy` and `groupBy` doesn't make sense on a string. You should probably read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) It probably was a JSON string before you parsed it to a JavaScript object. – jabaa Feb 14 '22 at 15:09
  • I was under the impression that json that is handled like json, IE. Not a string, where you access values and keys by dot notation, was regarded as an object. In much the same way an array is regarded as an object. Might be my misunderstanding. – Strontium_99 Feb 14 '22 at 15:13
  • Yes, that's a misunderstanding. You can't access the keys and values of JSON in JavaScript, because JSON is a string in JavaScript. First, you have to parse it. After you've parsed it, it's a JavaScript object and not JSON anymore. – jabaa Feb 14 '22 at 15:15

1 Answers1

3

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>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209