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

197
Views
Combine objects in array in Javascript

I have an array of objects like follows

0: {bill_no: '0000633', test_code: '1', analyzer_code: '2'}

1: {bill_no: '0000633', test_code: '2', analyzer_code: '2'}

2: {bill_no: '0000633', test_code: '28', analyzer_code: '3'}

3: {bill_no: '0000633', test_code: '254', analyzer_code: '2'}

I want to combine those objects to get the following result (join the objects based on same analyzer_code).

0: {bill_no: '0000633', test_code: '1,2,254', analyzer_code: '2'}

1: {bill_no: '0000633', test_code: '28', analyzer_code: '3'}

How can I achieve this in Javascript?

Edit: bill_no will be same in the every object

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

0

Apparently you want to group by bill_no and analyzer_code.

One way is to use reduce and collect groups by a key that is the combination of bill_no and analyzer_code properties. I'll use JSON.stringify to create such a key:

let data = [
  {bill_no: '0000633', test_code: '1', analyzer_code: '2'},
  {bill_no: '0000633', test_code: '2', analyzer_code: '2'},
  {bill_no: '0000633', test_code: '28', analyzer_code: '3'},
  {bill_no: '0000633', test_code: '254', analyzer_code: '2'}
];

let result = Object.values(
    data.reduce((acc, {bill_no, test_code, analyzer_code}) => {
        let key = JSON.stringify([bill_no, analyzer_code]);
        if (acc[key]) acc[key].test_code += "," + test_code
        else acc[key] = {bill_no, test_code, analyzer_code};
        return acc;
    }, {})
);

console.log(result);

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!