0

I know reduce is the function I need to use but I don't know how to start. Maybe there is a more straightforward manner to achieve what I want? I want to group questions with "processUrlID" and with the same sectionID. How can I manipulate this object to create a new object?

[
  {
      "processUrlID": "9a0e6b48-4019-46ff-9513-eff55c8a0716",
      "processParent": "84050058-534d-4e60-92e6-95315786e360",
      "questions": [
          {
              "processTitle": "sdqf",
              "DATAINS_TXTSIN_checklistSection": "Section 1",
              "DATAINS_TXTSIN_checklistSectionID": "2d9c7bc3-c6f7-4204-a174-1fad80f206a7",
              "processUrlID": "da65e3aa-def0-4b69-bbca-7b0c3fe44572",
              "processParent": "9a0e6b48-4019-46ff-9513-eff55c8a0716",
          },
          {
              "processTitle": "qsdf",
              "DATAINS_TXTSIN_checklistSection": "Section 1",
              "DATAINS_TXTSIN_checklistSectionID": "2d9c7bc3-c6f7-4204-a174-1fad80f206a7",
              "processUrlID": "0cca5b1d-d5d8-4d5b-9f4c-9cb6ff92e4c6",
              "processParent": "9a0e6b48-4019-46ff-9513-eff55c8a0716",
          },
          {
              "processTitle": "qsdfsf",
              "DATAINS_TXTSIN_checklistSection": "Section 1",
              "DATAINS_TXTSIN_checklistSectionID": "2d9c7bc3-c6f7-4204-a174-1fad80f206a7",
              "processUrlID": "d2e70fc8-990b-4660-9d62-1ca0df0d64d3",
              "processParent": "9a0e6b48-4019-46ff-9513-eff55c8a0716",
          },
          {
            "processTitle": "dsezar",
            "DATAINS_TXTSIN_checklistSection": "Section 2",
            "DATAINS_TXTSIN_checklistSectionID": "a4bbde66-8ba0-4513-a64d-41fc34c7827c",
            "processUrlID": "d2e70fc8-990b-4660-9d62-1ca0df0d64d3",
            "processParent": "9a0e6b48-4019-46ff-9513-eff55c8a0716",
        }
      ],
      "sections": [
          {
              "processUrlId": "2d9c7bc3-c6f7-4204-a174-1fad80f206a7",
              "name": "Section 1",
              
          },
          {
              "processUrlId": "a4bbde66-8ba0-4513-a64d-41fc34c7827c",
              "name": "Section 2",
          }
      ]
  }
]

to become this object:

[
  {
    "section": "Section 1",
    "sectionID": "2d9c7bc3-c6f7-4204-a174-1fad80f206a7",
    "questions": [
      "da65e3aa-def0-4b69-bbca-7b0c3fe44572",
      "0cca5b1d-d5d8-4d5b-9f4c-9cb6ff92e4c6",
      "d2e70fc8-990b-4660-9d62-1ca0df0d64d3",
    ]
  },
  {
    "section": "Section 2",
    "sectionID": "a4bbde66-8ba0-4513-a64d-41fc34c7827c",
    "questions": [
      "d2e70fc8-990b-4660-9d62-1ca0df0d64d3"
    ]
  }
]
Michael
  • 1
  • 1
  • Welcome to Stack Overflow! Please take the [tour] if you haven't already (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO and elsewhere, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mre] showing your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Apr 15 '22 at 08:55
  • 1
    But a tip: Unless you're doing [functional programming](https://en.wikipedia.org/wiki/Functional_programming) with predefined, reusable reducer functions, no, `reduce` isn't the function you need to use. Just use a loop. Outside its FP realm, `reduce` is just an overcomplicated loop. – T.J. Crowder Apr 15 '22 at 08:56
  • 2
    please provide output based on input, I cant match any field – shutsman Apr 15 '22 at 08:59
  • 1
    Looks like a straightforward 'group by' [Group array items using object](https://stackoverflow.com/questions/31688459/group-array-items-using-object) – pilchard Apr 15 '22 at 09:04

0 Answers0