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);
};
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.