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

82
Vistas
Re-usable javascript function with variable object path name passed on as a parameter?

I am trying to write a re-usable javascript function to insert key/value pairs into variable path locations within an object. Assume I have an object like this.

const obj ={
  firstName: 'Bob',
  lastName: 'Smith'
}

I have created a re-usable function like so

function mapData(doc, path) {
  const keyNames = Object.keys(doc);
  const bigDoc = {};

  keyNames.forEach(keyname => {
    bigDoc[keyname] = doc[keyname];
  });

  return bigDoc;
}

Now I want to be able to place it under the correct path under bigDoc, like for example bigDoc.teacher.math or bigDoc.student.yearten by calling the method mapData(obj, 'teacher.math') or mapData(obj, 'student.yearten') so that, for example, bigDoc.teacher.math.firstName is set to 'Bob. I know how to do it with one level deep, but I don't know how for two or more levels deep.

Please help.

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