Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Group objects based on similar value in array of objects

I have an array of objects I want to group/merge the objects which have similar branches and environments and concat their pools at the same time.

const data = [
  {
    branch: "master",
    environment: "dev",
    pool: "6g",
    service: "amex",
  },
  {
    branch: "master",
    environment: "dev",
    pool: "6g",
    service: "amex",
  },
  {
    branch: "feature/rest",
    environment: "dev",
    pool: "2g",
    service: "amex",
  },
  {
    branch: "master",
    environment: "dev",
    pool: "4g",
    service: "amex",
  },
  {
    branch: "hotfix/23",
    environment: "test",
    pool: "9g",
    service: "amex",
  },
  {
    branch: "hotfix/23",
    environment: "test",
    pool: "1g",
    service: "amex",
  },
];

I want the result in the below format removing duplicate objects as well I tried to reduce it but as array reduce returns a single object as a result and the other objects are being omitted from the response what data structure or way I can use to achieve the result?

const result = [
  {
    branch: "master",
    environment: "dev",
    pool: "6g, 4g",
    service: "amex",
  },
  {
    branch: "feature/rest",
    environment: "dev",
    pool: "2g",
    service: "amex",
  },
  {
    branch: "hotfix/23",
    environment: "test",
    pool: "9g,1g",
    service: "amex",
  },
];
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const data = [{
    branch: "master",
    environment: "dev",
    pool: "6g",
    service: "amex",
  },
  {
    branch: "feature/rest",
    environment: "dev",
    pool: "2g",
    service: "amex",
  },
  {
    branch: "master",
    environment: "dev",
    pool: "4g",
    service: "amex",
  },
  {
    branch: "hotfix/23",
    environment: "test",
    pool: "9g",
    service: "amex",
  },
  {
    branch: "hotfix/23",
    environment: "test",
    pool: "1g",
    service: "amex",
  },
];

var cacheMix = {};

for (var i = 0; i < data.length; i++) {

  var item = data[i];
  var compositeKey = item.environment + "~" + item.branch;

  if (cacheMix[compositeKey]) {
    cacheMix[compositeKey].pools[item.pool] = 1;
  } else {
    var pools = {}; pools[item.pool] = 1; //to avoid dublicate pools 
    cacheMix[compositeKey] = {
      branch: item.branch,
      environment: item.environment,
      service: item.service,
      pools: pools 
    }
  }
}


var result = [];


for (var key in cacheMix) {

  var item = cacheMix[key];
  result.push({
    branch: item.branch,
    environment: item.environment,
    service: item.service,
    pool: Object.keys(item.pools).join(", ")
  });
}

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Just create a dictionary out of them and fill in the values.

const data=[{branch:"master",environment:"dev",pool:"6g",service:"amex"},{branch:"feature/rest",environment:"dev",pool:"2g",service:"amex"},{branch:"master",environment:"dev",pool:"4g",service:"amex"},{branch:"hotfix/23",environment:"test",pool:"9g",service:"amex"},{branch:"hotfix/23",environment:"test",pool:"1g",service:"amex"},]


let x = {};
data.forEach(y => x[y.branch + "|" + y.environment] = y);

var res = Object.values(x).map(y => Object.assign({}, y)).map(y => 
{
    y.pool = data.filter(d => d.branch == y.branch && d.environment == y.environment).map(x => x.pool).join(",");
   return y;
})

If you do not care about immutability(original objects in data change), then remove Object.assign map and it will yield the same results

about 4 years ago · Juan Pablo Isaza Relatório

0

data.reduce((prev, curr) => {
  const container = prev.find(
    el => (el.branch === curr.branch) && (el.environment === curr.environment)
  );
  if (container) {
    container.pool = container.pool + `,${curr.pool}`
    return prev;
  } else {
    return prev.concat({...curr}) 
  }
}, [])

but I would suggest to instead create an object with keys equal to branch names.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda