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

87
Views
Filter object array based on category and return list of matches React TS

I am having trouble filtering an object array, based on a given property.

The object array looks something like this:

someData: [{
                id: "123",
                personData: [
                  {
                    personName: "John Smith",
                    personId: "125443",
                    categoryId: "B1",
                    description: "description"
                  }}]}];

I want to group the personData based on the categoryId, but represent the category grouping by its categoryName (instead of Id as this will not make much sense to the user).

There are a series of categories which are contained in a json in the following format:

const groups = {
  data: [
    {
      categoryName: "Banking",
      categoryId: "B1",
      description: "Financial sector"
    },
    {
      categoryName: "Retail",
      categoryId: "R1",
      description: "Retail and customer experience"
    }
  ]
};

How would I go about filtering based on these conditions? All help is much appreciated! I am using react and typescript

Once filtered the data should look like this:

Banking
    personName: John Smith
    personId: 125443

Retail
   personName: Judie Smith
   personId: 124938
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think something along the lines of this function would return what you need.

const data = [
    {
      categoryName: "Banking",
      categoryId: "B1",
      description: "Financial sector"
    },
    {
      categoryName: "Retail",
      categoryId: "R1",
      description: "Retail and customer experience"
    }
];

function groupByProperty(arrayOfObjects, property) {
  return arrayOfObjects.reduce((acc, curr) => {
    const key = curr[property];
    if (!acc[key]) {
      acc[key] = [];
    }
    acc[key].push(curr);
    return acc;
  }, {});
}

const dataByCategoryName = groupByProperty(data, "categoryName");
console.log(dataByCategoryName);

/* Output
{
    Banking: [{
        categoryId: "B1",
        categoryName: "Banking",
        description: "Financial sector"
    }],
    Retail: [{
        categoryId: "R1",
        categoryName: "Retail",
        description: "Retail and customer experience"
    }]
}
*/
about 4 years ago · Juan Pablo Isaza Report

0

You can try groupBy on that person data.

const products = [
 { name: 'apples', category: 'fruits' },
 { name: 'oranges', category: 'fruits' },
 { name: 'potatoes', category: 'vegetables' }
];

const groupByCategory = products.groupBy(product => {
    return product.category;
});

console.log(groupByCategory);
// {
//   'fruits': [
//     { name: 'apples', category: 'fruits' }, 
//     { name: 'oranges', category: 'fruits' },
//   ],
//   'vegetables': [
//     { name: 'potatoes', category: 'vegetables' }
//   ]
// }
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!