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

236
Views
How to group and concat values by keys in JS

I try like a maniac to do some kind of a merge / sort by key on an array of object. Don't know why but I cannot figure how to do that.

My app is in Ionic / Angular.

Here is what I have :

[
  {
      "2021": {
          "a": "aText"
      }
  },
  {
      "2021": {
          "b": "bText"
      }
  },
  {
      "2020": {
          "z": "zText"
      }
  },
  {
      "2020": {
          "y": "yText"
      }
  },
  {
      "2020": {
          "x": "xText"
      }
  }
]

My goal is to get this :

[
  {
      "2021": { "a": "aText", "b": "bText" }
  },
  {
      "2020": { "z": "zText", "y": "yText", "x": "xText" }
  }
]

In other words, I'd like to regroup by year and concat them.

Does anybody have an idea on how to do that ?

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

0

If you just want a single object with years as keys then it's just a standard 'group by' with a nested loop to iterate the Object.entries() of each object. If you do want your originally posted output (an array of individual objects) you can just map() the entires of the returned grouped object and convert each to an object using Object.fromEntries()

const input = [
  { 2021: { a: 'aText' } },
  { 2021: { b: 'bText' } },
  { 2020: { z: 'zText' } },
  { 2020: { y: 'yText' } },
  { 2020: { x: 'xText' } },
];

const grouped_object = input.reduce(
  (a, o) => (Object.entries(o).forEach(([y, o]) => (a[y] = { ...(a[y] ?? {}), ...o })), a),
  {}
);

// if you just want a single object with years as keys
console.log(grouped_object);

const grouped_array = Object.entries(grouped_object)
  .map(([year, data]) => ({[year]: data}));

// the output from your question
console.log(grouped_array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Or refactored to use a for...of loop and Object.assign()

const input = [
  { 2021: { a: 'aText' } },
  { 2021: { b: 'bText' } },
  { 2020: { z: 'zText' } },
  { 2020: { y: 'yText' } },
  { 2020: { x: 'xText' } },
];

const grouped_object = {};
for (const obj of input) {
  for (const [year, data] of Object.entries(obj)) {
    grouped_object[year] = Object.assign(grouped_object[year] ?? {}, data);
  }
}

// if you just want a single object with years as keys
console.log(grouped_object);

// or avoiding computed properties
const grouped_array = Object.entries(grouped_object)
  .map(([year, data]) => (o={}, o[year]=data, o));

// the output from your question
console.log(grouped_array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

It would be better to use an object to group by year. This is an example that uses reduce to iterate over the array to produce that object.

const data=[{2021:{a:"aText"}},{2021:{b:"bText"}},{2020:{z:"zText"}},{2020:{y:"yText"}},{2020:{x:"xText"}}];

const out = data.reduce((acc, obj) => {

  // Get the key and value from the object that in
  // the current iteration
  const [ [ key, value ] ] = Object.entries(obj);

  // If the key doesn't exist on the accumulator (the initial
  // object that we passed into the `reduce`) create an empty object
  acc[key] = acc[key] || {};

  // Update the value of that object property with
  // the value of the object
  acc[key] = { ...acc[key], ...value };

  // Return the updated object for the next iteration
  return acc;

// Here's the initial object that
// acts as the accumulator through all the iterations
}, {});

console.log(out);

Or using an array to hold the information for each year:

const data=[{2021:{a:"aText"}},{2021:{b:"bText"}},{2020:{z:"zText"}},{2020:{y:"yText"}},{2020:{x:"xText"}}];

const out = data.reduce((acc, obj) => {

  const [ [ key, value ] ] = Object.entries(obj);

  // Use an array instead of an object
  acc[key] = acc[key] || [];

  // Push the first element of the Object.values
  // into the array
  acc[key] = [ ...acc[key], Object.values(value)[0] ];

  return acc;

}, {});

console.log(out);

Additional documentation

  • Object.entries

  • Object.values

  • Destructuring assignment

  • Spread syntax

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!