Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
¿Cómo puedo fusionar dos claves dentro del mismo objeto en javascript?

Entrada :

 const array=[{key: '1', value: 'VALUE1'},{key: '2', value: 'VALUE2'},{key: '1', value: 'VALUE3'},{key: '3', value: 'VALUE4'}]

esperado :

 const array=[{key: '1', value: 'VALUE1','VALUE3'},{key: '2', value: 'VALUE2'},{key:'3', value: 'VALUE4'}]
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Espero que la siguiente función resuelva su problema:

 var arvConcat = (r) =>{ var rr=[],or=[]; for(let ri of r){ if(typeof rr[ri.key] === 'undefined')rr[ri.key]=[]; rr[ri.key].push(ri.value); } for(let ri in rr){ or.push({key:ri,value:rr[ri]}); } return or; } let r = [{ key: '1', value: 'VALUE1' }, { key: '2', value: 'VALUE2' }, { key: '1', value: 'VALUE3' }, { key: '3', value: 'VALUE4' }]; let newR = arvConcat(r); console.log(newR);

Gracias

about 4 years ago · Santiago Trujillo Denunciar

0

Puedes lograr esto usando un bucle for normal

 let array = [{ key: '1', value: 'VALUE1', }, { key: '2', value: 'VALUE2', }, { key: '1', value: 'VALUE3', }, { key: '3', value: 'VALUE4', }, ]; let i = 0; while (i < array.length) { if (i + 1 <= array.length) { for (let x = i + 1; x < array.length; x++) { if (array[i].key === array[x].key) { array[i].value = `${array[x].value} ${array[i].value}`; array.splice(x, 1); } } i++ } } console.log(array);

about 4 years ago · Santiago Trujillo Denunciar

0

Una forma de hacerlo es usar Object.values y reducir

 //Input: const array = [{ key: '1', value: 'VALUE1' }, { key: '2', value: 'VALUE2' }, { key: '1', value: 'VALUE3' }, { key: '3', value: 'VALUE4' }] const result = Object.values(array.reduce((a,o) => { a[o.key] = a[o.key] || {'key': o.key, 'value' : []}; a[o.key]['value'].push(o.value); return a; }, {})); console.log(result);

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda