Reputation: 21
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);
};
Upvotes: 0
Views: 731
Reputation: 274
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
Upvotes: 1