admin管理员组文章数量:1336213
So this is very weird, I have a foreach function like this:
let cookieValue = '';
cookieList.forEach(function(cookieItem) {
const cookieParts = cookieItem.split('=');
const value = cookieParts[1];
const key = cookieParts[0];
if (key.trim() === cookieName) {
cookieValue = value;
return cookieValue;
}
});
return cookieValue;
which works fine, however when I change the lines inside the if statement to a single line:
return value;
It returns undefined always.
Any ideas of what can be happening here?
So this is very weird, I have a foreach function like this:
let cookieValue = '';
cookieList.forEach(function(cookieItem) {
const cookieParts = cookieItem.split('=');
const value = cookieParts[1];
const key = cookieParts[0];
if (key.trim() === cookieName) {
cookieValue = value;
return cookieValue;
}
});
return cookieValue;
which works fine, however when I change the lines inside the if statement to a single line:
return value;
It returns undefined always.
Any ideas of what can be happening here?
Share Improve this question asked May 27, 2016 at 23:59 RicardoERicardoE 1,7256 gold badges24 silver badges42 bronze badges 4-
10
The system pletely ignores values returned from
.forEach()
callbacks. – Pointy Commented May 28, 2016 at 0:02 - 1 How did you expect it to behave? – Oriol Commented May 28, 2016 at 0:02
- And what exactly are you trying to do? – charlietfl Commented May 28, 2016 at 0:05
-
Take this simplified but equivalent example:
function foo() { function bar() { return 42; }; return 21; }
. What doesfoo()
return? – Felix Kling Commented May 28, 2016 at 0:12
3 Answers
Reset to default 2The return of forEach is ignored but you can use map and filter:
function getCookieValue(cookieList, cookieName) {
var val = cookieList.map(function(cookieItem) {
var cookieParts = cookieItem.split('=');
var value = cookieParts[1];
var key = cookieParts[0];
return (key.trim() === cookieName) ? value : null;
})
.filter((value) => { return value != null })[0];
return val;
}
let cookieValue = getCookieValue(["key1=val1", "key2=val2"], "key2"); // > "val2"
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 bination 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;
Your return value in the forEach function return in that function. By placing the return in the outer function, you return that value when that function is called. See this simplified example.
function x(){
function y(){
return 5 // Does not return in the x function
}
y() // = 5
return 7
}
x() // =7
You seem like you are looking for Array.find.
let cookieValue = '';
return cookieList.find(function(cookieItem) {
const cookieParts = cookieItem.split('=');
const value = cookieParts[1];
const key = cookieParts[0];
return key.trim() === cookieName
});
本文标签: javascriptreturn value inside foreachStack Overflow
版权声明:本文标题:javascript - return value inside foreach - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742245213a2439093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论