So this is very weird, I have a foreach function like this:
let cookieValue = \'\';
cookieList.forEach(function(cookieItem) {
const cookieParts = co
Your code works "fine" at first because you're manually changing the value of cookieValue.
Array.prototype.forEach doesn't do anything with the returning value of the callback you pass to it.
For this case, I'd use a combination of Array.prototype.map and Array.prototype.reduce:
let cookieValue = cookieList.map(function(cookieItem) {
const cookieParts = cookieItem.split('=');
const value = cookieParts[1];
const key = cookieParts[0];
if (key.trim() !== cookieName) {
return null;
}
return value;
}).reduce(function(a, b) {
return a || b;
}, '');
return cookieValue;