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

176
Vistas
Function to convert array of strings into array of objects

I am having trouble with a function question for my course.

Write a function that converts an array of strings into an array of objects, with the supplied key

E.g.

arrayToObjects(["Mike", "Emily"], "name") // => [{ name: "Mike" }, { name: "Emily"}]
function arrayToObjects(array, key) {
    const objArr = [] // to return an array later.
    this.key = key
    for (let i = 0; i < array.length; i++) {
        const o = new Object()
        o.key = array[i]
        objArr.push(o)
        return objArr
    }
}

arrayToObjects(['Mike', 'Emily'], 'name')

If I pass the array values and key string I log this: [ { key: 'Mike' }, { key: 'Emily' } ]

I can't get the key name into the constructor from the function argument.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Change the following line :

o.key = array[i] 
//change
o[key] = array[i]
function arrayToObjects(array, key) {
    const objectArray = []; // To return an array later.
    this.key = key;
    for (const element of array) {
        const o = new Object();
        o[key] = element;
        objectArray.push(o);
    }
   return objectArray;
}
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