3

I currently have an array of items that looks a bit like this: I want to group the items by category lookup, with the slight problem that category lookup is potentially an array, such that Parent Item 2 would be listed twice (once in My Cat) and once in something else) I tried using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/groupBy but it doesn't seem to be able to handle this?

     [
           {
                "tool_id": "4-19de-454673d9-9ef5-4545",
                "somekey" : "Parent Item 2"
                "categoryLookup": [
                    {
                        "category_name": "New Item",
                    }
                ]
            },
            {
                "tool_id": "be7ea707-19de-43d9-9ef1-d4a3ff79f77a",
                "somekey" : "Parent Item"
                "categoryLookup": [
                    {
                        "category_name": "My Cat",
                    },
                    {
                        "category_name": "Something Else",
                    }
                ]
            }
     ]

The final result would look something like:

    [
       {
           New Item: [
              {...contains 4-19de-454673d9-9ef5-4545 }
           ],
           My Cat: [
              {...contains be7ea707-19de-43d9-9ef1-d4a3ff79f77a}
           ],
           Something Else: [
              {... contain be7ea707-19de-43d9-9ef1-d4a3ff79f77a} 
           ]
        }
    ]
5
  • .groupBy() only allows one key per element, so no, it would not work. Commented Apr 29, 2022 at 13:08
  • ... also it's only supported by Firefox, apparently Commented Apr 29, 2022 at 13:09
  • What would the expected result look like if more than item belongs to a certain category? Commented Apr 29, 2022 at 13:11
  • And how is this wrong? You'd want to find the Parent Item when looking up either category, right? What output are you trying to get? Commented Apr 29, 2022 at 13:11
  • Could you just loop over the original array and construct the final result? Also I guess the same category_name could be in multiple objects and thus giving you more than one object in some of the final arrays... Commented Apr 29, 2022 at 13:17

1 Answer 1

3

You can iterate over the original array and create the final one:

var data = [{
    "tool_id": "4-19de-454673d9-9ef5-4545",
    "somekey": "Parent Item 2",
    "categoryLookup": [{
      "category_name": "New Item",
    }]
  },
  {
    "tool_id": "be7ea707-19de-43d9-9ef1-d4a3ff79f77a",
    "somekey": "Parent Item",
    "categoryLookup": [{
        "category_name": "My Cat",
      },
      {
        "category_name": "Something Else",
      }
    ]
  }
];

function groupByCategory(data) {
  const res = {};

  data.forEach(item => {
    item.categoryLookup.forEach(category => {
      const name = category.category_name;
      res[name] ??= [];
      res[name].push({
        item: item.tool_id //or all the properties you want
      });
    });
  });
  return res;
}

console.log( groupByCategory(data) );

Sign up to request clarification or add additional context in comments.

4 Comments

Might be res[name] ?? []; instead @Lee Taylor
@LeeTaylor please go to the edit history and you can see that YOU changed it to ? ?, it was correct before your edit!!
@AmirMB Apologies. It seems that clicking the "Tidy" button in the snippet editor does that. Sorry again.
@LeeTaylor no worries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.