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

170
Views
Grouping array of objects to object of arrays by multiple keys and adding the id's of the group to the key

Made sample of code -

Interface -

interface IInterface {
  id: number;
  name: string;
  age: number;
}

array -

const arr: IInterface[] = [
    {
        id: 1,
        name: 'daniel',
        age: 12
    },
    {
        id: 2,
        name: 'jonny',
        age: 13
    },
    {
        id: 3,
        name: 'daniel',
        age: 12
    },
    {
        id: 4,
        name: 'daniel',
        age: 12
    }
]

GroupBy function -

const GroupBy = (
  array: IInterface[],
  f: (element: IInterface) => (string | number | undefined)[]
) => {
  const groups: { [key: string]: IInterface[] } = {};

  array.forEach((object) => {
    const group = f(object).join("-");

    groups[group] = groups[group] || [];
    groups[group].push(object);
  });
  return groups;
};

Using the groupBy function -

const Grouped = GroupBy(arr, (element: IInterface) => {
        return [
          element.name, element.age,
        ];
      });

Result now -

{
  "daniel-12": [
    {
      "id": 1,
      "name": "daniel",
      "age": 12
    },
    {
      "id": 3,
      "name": "daniel",
      "age": 12
    },
    {
      "id": 4,
      "name": "daniel",
      "age": 12
    }
  ],
  "jonny-13": [
    {
      "id": 2,
      "name": "jonny",
      "age": 13
    }
  ]
}

Wanted result -

{
  "daniel-12-ids-1-3-4": [
    {
      "id": 1,
      "name": "daniel",
      "age": 12
    },
    {
      "id": 3,
      "name": "daniel",
      "age": 12
    },
    {
      "id": 4,
      "name": "daniel",
      "age": 12
    }
  ],
  "jonny-13-ids-2": [
    {
      "id": 2,
      "name": "jonny",
      "age": 13
    }
  ]
} 

Need that all the group id's will by at the key name .......................................................................................................................................................

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could create a new object from the old one.

const
    grouped = { "daniel-12": [{ id: 1, name: "daniel", age: 12 }, { id: 3, name: "daniel", age: 12 }, { id: 4, name: "daniel", age: 12 }], "jonny-13": [{ id: 2, name: "jonny", age: 13 }] },
    result = Object.fromEntries(Object.entries(grouped).map(([k, v]) => [
        [k, 'ids', v.map(({ id }) => id).join('-')].join('-'),
        v
    ]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!