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

254
Views
Create array of multiple objects with different structure from array of objects with duplicate keys using Object.assign() in JavaScript

I have an initial array of objects in the following structure:

let sampleArray = [
  { chairperson: 'Sample person' },
  { chairperson: 'Sample person 2' },
  { arbitratorClaimant: 'Sample person 3' },
  { arbitratorRespondent: 'Sample person 4' },
  { secretary: 'Sample person 5' }
]

I am using

Object.assign({}, ...sampleArray)

to combine the array of objects into a single object, but since the object cannot have duplicate keys, it overwrites the first chairperson ('Sample person') with the second chairperson. Instead, I would like to output an array with multiple objects where objects with duplicate keys are put into the next object in the array. For instance, this is what I am looking for:

[
  {
    chairperson: 'Sample person',
    arbitratorClaimant: 'Sample person 3',
    arbitratorRespondent: 'Sample person 4',
    secretary: 'Sample person 5'
  },
  {
    chairperson: 'Sample person 2'
  }
]

Is this possible?

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

0

This is - not yet - an answer but an attempt to discuss the desired result.

How about creating a result like the following:

  {
    chairperson: ['Sample person','Sample person 2'],
    arbitratorClaimant: 'Sample person 3',
    arbitratorRespondent: 'Sample person 4',
    secretary: 'Sample person 5'
  }

Looking at your comment, maybe the following could work for you:

  {
    chairperson: 'Sample person, Sample person 2',
    arbitratorClaimant: 'Sample person 3',
    arbitratorRespondent: 'Sample person 4',
    secretary: 'Sample person 5'
  }

Instead of the ", " any other separator could be chosen for lists of multiple names

about 4 years ago · Juan Pablo Isaza Report

0

In this example collated is similar to @CarstenMassmann's first alternative data structure. This is quite possibly an over-involved way of doing it but the second part of the code essentially pivots (I think that's the right term) the data in collated so that it can be used for table rows.

let sampleArray = [
  { chairperson: 'Sample person' },
  { chairperson: 'Sample person 2' },
  { arbitratorClaimant: 'Sample person 3' },
  { arbitratorRespondent: 'Sample person 4' },
  { secretary: 'Sample person 5' }
];

const merge_objects = (obj1, obj2) =>
  Object.entries(obj2).reduce(
    (acc, [key, val]) =>
      Object.assign(acc, { [key]: (acc[key] ?? []).concat(val) }),
    obj1
  );

const collated = sampleArray.reduce(
  (acc, val) => merge_objects(acc, val),
  {}
);

const max_item_count = (input) =>
  Math.max(
    ...Object.values(input).map((arr) => arr.length)
  );

const get_table_rows = (input) =>
  Array
    .from({ length: max_item_count(input) })
    .map(
      (row, index) =>
        Object.fromEntries(
          Object.entries(collated)
            .map(([key, val]) => [key, collated[key][index] ?? null])
            .filter(([key, val]) => val)
        )
    );

console.log( JSON.stringify(get_table_rows(collated)) );

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!