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
Add a ment  | 

4 Answers 4

Reset to default 3

return 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