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

128
Views
Javascript Grouping by object property, when property is an array

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} 
           ]
        }
    ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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) );

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!