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

170
Vistas
How to use object key from string value in javascript

I have sample object, need to reassign the value dynamically.

here is sample object, i have stored the object in dynamodb with inarray data type. i have formed the object by using loop. i couldn't use the payload.offer value. kindly suggest the better solution.

{
  organization_id: 4002400004,
  organization_name: 'Velocity Global Integration Sandbox',
  'payload.offer': 'payload offer',
}

Required output format

    {
      organization_id: 4002400004,
      organization_name: 'Velocity Global Integration Sandbox',
      'payload': {
        offer: 'payload offer'
    },
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

As I understand, you want a way to convert from dot-notation to deeply-nested objects. You can try the following then:

function dotToNested(obj) {
  return Object.entries(obj).reduce((newObj, [key, value]) => {
    const path = key.split(".");

    // This part makes it recursive, you can just use value if you don't want that
    const newValue = typeof value === "object" && !!value
      ? dotToNested(value)
      : value;
    
    if (path.length > 1) {
      // Last key will be used for assignment
      const newKey = path.pop();

      // Get the object to assign to, and create the intermediate objects along the way
      const objRef = path.reduce((ref, key) => {
        // create the intermediate object
        ref[key] = {};

        // move further down in the deeply nested structure
        return ref[key];
      }, newObj);
      
      objRef[newKey] = newValue;
    } else {
      // Just a "regular" key, assign directly to it, no need for the path
      newObj[key] = newValue;
    }

    return newObj;
  }, {});
}

const dotNotation = {
  organization_id: 4002400004,
  organization_name: 'Velocity Global Integration Sandbox',
  'payload.offer': 'payload offer',
};

const deeplyNested = dotToNested(dotNotation);

console.log({ dotNotation, deeplyNested });

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the top level method Object.keys() from the standard Object prototype, and split('.') the target key, and try to delete the old key and add the new key.

Well, this is my code suggestion


const input = {
    organization_id: 4002400004,
    organization_name:
        'Velocity Global Integration Sandbox',
    'payload.offer': 'payload offer',
};


function modify(input, requireKey) {
    const newObject = {};
    Object.keys(input).forEach((key) => {
        if (key === requireKey) {
            let [newKey, newDownLevelKey] = key.split('.');

            delete Object.assign(newObject, input, {
                [newKey]: input[key],
            })[key];

            newObject[newKey] = {
                [newDownLevelKey]: newObject[newKey],
            };
        }
    });
    return newObject;
}

console.log(modify(input, 'payload.offer'));

Resources :

Object.assign() : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Object.keys() : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

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