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

135
Views
javascript I want to make an array of tags with different overlapping values in objectArray

Before

  const array = [
    { group: '1', tag: ['sins'] },
    { group: '1', tag: ['sun'] },
    { group: '2', tag: ['red'] },
    { group: '2', tag: ['blue'] },
    { group: '2', tag: ['black'] },
  ];

After

  const array = [
    { group: '1', tag: ['sins', 'sun'] },
    { group: '2', tag: ['red', 'blue', 'black'] },
  ];

I want to change it like the chord above. I want someone to create a cool chord.

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

0

You can convert the array to an object using reduce and convert it back to an array.

const array = Object.entries([
    { group: '1', tag: ['sins'] },
    { group: '1', tag: ['sun'] },
    { group: '2', tag: ['red'] },
    { group: '2', tag: ['blue'] },
    { group: '2', tag: ['black'] },
  ].reduce((acc, { group, tag }) => ({ ...acc, [group]: acc[group] ? acc[group].concat(tag) : tag}), {})).map(([group, tag]) => ({ group, tag }));
  
console.log(array);

about 4 years ago · Juan Pablo Isaza Report

0

Please read the docs for the javascript array. https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array

you have several methods to solve your problem.

about 4 years ago · Juan Pablo Isaza Report

0

 const array = [
    { group: '1', tag: ['sins'] },
    { group: '1', tag: ['sun'] },
    { group: '2', tag: ['red'] },
    { group: '2', tag: ['blue'] },
    { group: '2', tag: ['black'] },
  ];
  
  // using reduce to create dictionary and taking group value as key:

let groupDicionry = array.reduce((dic, obj) => {
// create object if already not in dictionary
if(!dic[obj.group]) { dic[obj.group] =  { group: obj.group, tag: [] };  };
dic[obj.group].tag.push(obj.tag[0]);
return dic
}, {});

let result  = Object.values(groupDicionry);

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!