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

173
Visualizações
Convert array of objects to string based on condition in JavaScript

im trying to convert array of objects to string. I want to concatenate col3: "R20;R301". In debugging i can able to see concatenated string in obj before pushing. But after pushing, it shows only one string like R20.

var array1 = [
  { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'},
  { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'},
  { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'},
  { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'},
  { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'},
  { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'},
  { col1: 'RED4+R3', col2: 'RED', col3: 'D20'},
  { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}];
  
var result = array1.reduce(function (r, a) {
    let obj = r.find(el => el.col2 === a.col2);
    const text1 = {
        "text1": a.col1
    };
    var text2 = a.col3;
    if (obj) {
        if (!obj.col1.find(el => el.text1 === text1.text1)) {
            obj.col1.push(text1);
        }
        if (obj.col3 !== text2) {
            text2 = obj.col3 + ";" + text2;
        }
    } else {
        obj = {
            col1: [text1],
            col2: a.col2,
            col3: text2
        }
        r.push(obj)
    }
    return r;
}, []);

console.log(result);

I tried the above script, but couldn't achieve. An expected output should be like below. Thanks in advance.

var result = [{
        "col1": [{
                "text1": "ABC1+R2"
            },
            {
                "text1": "ABC1+R3"
            }
        ],
        "col2": "ABC",
        "col3": "R20;R301"
    },
    {
        "col1": [{
                "text1": "CDE2+R4"
            },
            {
                "text1": "CDE2+R5"
            }
        ],
        "col2": "CDE",
        "col3": "R20"
    },
    {
        "col1": [{
            "text1": "RED4+R3"
        }],
        "col2": "RED",
        "col3": "D20"
    },
    {
        "col1": [{
            "text1": "GTR5+R2"
        }],
        "col2": "GTR",
        "col3": "R20"
    }
]
console.log(result);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you assign text2 = obj.col3 + ";" + text2;, you never update obj.col3 to hold that concatenated string.

var array1 = [
  { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'},
  { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'},
  { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'},
  { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'},
  { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'},
  { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'},
  { col1: 'RED4+R3', col2: 'RED', col3: 'D20'},
  { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}];

var result = array1.reduce(function(r, a) {
  let obj = r.find(el => el.col2 === a.col2);
  const text1 = {
    "text1": a.col1
  };
  var text2 = a.col3;
  if (obj) {
    if (!obj.col1.find(el => el.text1 === text1.text1)) {
      obj.col1.push(text1);
    }
    if (obj.col3 !== text2) {
      obj.col3 += ";" + text2;
    }
  } else {
    obj = {
      col1: [text1],
      col2: a.col2,
      col3: text2
    }
    r.push(obj)
  }
  return r;
}, []);

console.log(result);

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