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

83
Vistas
How to split an array of strings & return a single object with key/value pairs

I have this array of strings:

[ 
  'Back to Main Window: Retour à la fenêtre principale',
  'All Client Groups: Tous les groupes de clients',
  'Filter by Client: Filtrer par client' 
]

I would like to transform it into an object with key/value pairs like so:

{
   'Back to Main Window': 'Retour à la fenêtre principale',
   'All Client Groups': 'Tous les groupes de clients',
   'Filter by Client': 'Filtrer par client' 
}

I have to tried to use map() & split(), but I get this output instead:

const results = translations.map(translation => {
  const [key, value] = translation.split(':');

  return { key: value };
});

// results returns an "array" with the "key" word as key for all values :(
// [ { key: ' Retour à la fenêtre principale' },
//   { key: ' Tous les groupes de clients' },
//   { key: ' Filtrer par client' } ]
// 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

map over the array and split each item by ': ', then use Object.fromEntries:

const arr = [ 
  'Back to Main Window: Retour à la fenêtre principale',
  'All Client Groups: Tous les groupes de clients',
  'Filter by Client: Filtrer par client' 
]


const res = Object.fromEntries(arr.map(e => e.split(": ")))

console.log(res)

about 4 years ago · Juan Pablo Isaza Denunciar

0

An alternative solution to @Spectric would be to use reduce, to transform your array to an object.

const arr = [ 
  'Back to Main Window: Retour à la fenêtre principale',
  'All Client Groups: Tous les groupes de clients',
  'Filter by Client: Filtrer par client' 
];

function transform(arr) {
  return arr.reduce((obj, line) => {
    const [key, value] = line.split(': ');
    obj[key] = value;
    return obj;
  }, {});
}

console.log(transform(arr));

about 4 years ago · Juan Pablo Isaza 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