Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda