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

94
Vistas
Call or access nested const inside variable in Javascript

i'm newbie in javascript but i need help, can i access or call from outside a variable inside const block ? , from code in bottom, i need to access layerAttributes which the variable is inside addAttributes block, i try to console.log(layerAttributes); inside block is working, but i don't know how to call it from outside block, biggest appreciate for help and thank's in advance.

const addAttributes = (_element) => {
  let selectedElement = _element.layer;
  const layerAttributes = {
    trait_type: _element.layer.trait,
    value: selectedElement.traitValue,
    ...(_element.layer.display_type !== undefined && {
      display_type: _element.layer.display_type,
    }),
  };
  console.log(layerAttributes);
  if (
    attributesList.some(
      (attr) => attr.trait_type === layerAttributes.trait_type
    )   
  )
    return;
  attributesList.push(layerAttributes);
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As far as I understood you want to be able to access layerAttributes from outside the block, you can declare it outside then use it inside the const block and outside like this:

let layerAttributes = {}; //declared outside the block
    
    const addAttributes = (_element) => {
      let selectedElement = _element.layer;
      layerAttributes = { //accessed inside the block
        trait_type: _element.layer.trait,
        value: selectedElement.traitValue,
        ...(_element.layer.display_type !== undefined && {
          display_type: _element.layer.display_type,
        }),
      };
      console.log(layerAttributes);
      if (
        attributesList.some(
          (attr) => attr.trait_type === layerAttributes.trait_type
        )   
      )
        return;
      attributesList.push(layerAttributes);
    
    }; //end of block

    console.log(layerAttributes); //accessed outside the block

tell me if this is what you meant

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