Can somebody please tell me why this code works perfectly fine:
function getImage(images, path){
path = path.slice(0, path.lastIndexOf("/"));
let res;
images.forEach(img => {
let imgPath = img.node.absolutePath.slice(0, img.node.absolutePath.lastIndexOf("/"))
if (imgPath === path){
console.log(img.node)
res = img.node;
}
});
return res;
}
It returns the correct img.node object that I am looking for.
If I replace the "res = img.node" with "return img.node", it will always tell me the value is undefined.