admin管理员组文章数量:1301555
In the following code, can somebody explain why object property is returned using a for loop but not using a forEach. Is it something related to returning object references or is it something specific to forEach loop on an array ?
var empModule = (function(){
var empArray = [{
"name":"Aish",
"age":"27",
"location": "All"
},{
"name":"Anu",
"age":"26",
"location": "Muz"
},{
"name":"Vern",
"age":"25",
"location": "Mang"
}];
var searchAge = function(name){
for(var i=0;i<empArray.length;i++) {
if(empArray[i].name === name) {
return empArray[i].age;
}
};
};
var searchLocation = function(name){
empArray.forEach(function(obj){
if(name === obj.name) {
return obj.location;
}
});
};
return {
findAge: searchAge,
findLocation: searchLocation
};
})();
var secAge = empModule.findAge("Anu");
console.log(secAge); // Correct Output
var thirdLoc = empModule.findLocation("Vern");
console.log(thirdLoc); // Returns undefined
In the following code, can somebody explain why object property is returned using a for loop but not using a forEach. Is it something related to returning object references or is it something specific to forEach loop on an array ?
var empModule = (function(){
var empArray = [{
"name":"Aish",
"age":"27",
"location": "All"
},{
"name":"Anu",
"age":"26",
"location": "Muz"
},{
"name":"Vern",
"age":"25",
"location": "Mang"
}];
var searchAge = function(name){
for(var i=0;i<empArray.length;i++) {
if(empArray[i].name === name) {
return empArray[i].age;
}
};
};
var searchLocation = function(name){
empArray.forEach(function(obj){
if(name === obj.name) {
return obj.location;
}
});
};
return {
findAge: searchAge,
findLocation: searchLocation
};
})();
var secAge = empModule.findAge("Anu");
console.log(secAge); // Correct Output
var thirdLoc = empModule.findLocation("Vern");
console.log(thirdLoc); // Returns undefined
Share
asked Apr 8, 2015 at 7:59
Aishwarya SharmaAishwarya Sharma
331 gold badge1 silver badge3 bronze badges
4 Answers
Reset to default 3return
returns to the function it's in. In the for..
example, that's searchAge
. When you use forEach()
, you pass it a callback function, and so you return the value to that callback. You never return anything in searchLocation
.
You should just use the regular for..
loop both times here.
in java script there is no break method for forEach
.
if you use
return obj.location
it has no effect on it
but when you use return method in for loop then that will break and return the value.
There is some
and every
which has a break method.
Some
break on return true
and every
break on return false;
try like this
var location = "";
empArray.some(function (obj) {
if (name === obj.name) {
location = obj.location;
return true;
}
});
return location;
Or try like this
var location = "";
empArray.every(function (obj) {
if (name === obj.name) {
location = obj.location;
return false;
}
});.
return location;
That is because in the bellow code snippet
var searchLocation = function(name){
empArray.forEach(function(obj){
if(name === obj.name) {
return obj.location;
}
});
};
If you want to do the same thing use filter like bellow
var searchLocation = function(name){
return empArray.filter(function(obj) {
return name === obj.name
})[0].location;
};
the return
statement will be for anonymous function
you are giving as parameter to foreach
function not to function searchLocation
.
From https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
forEach() executes the callback function once for each array element; unlike every() and some(), it always returns the value undefined.
So you could try this which sets a variable and returns it after the loop:
var searchLocation = function(name){
var result;
empArray.forEach(function(obj){
if(name === obj.name) {
result = obj.location;
}
});
return result;
};
本文标签: foreach return object property javascriptStack Overflow
版权声明:本文标题:foreach return object property javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741677730a2391983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论