简体   繁体   中英

how to return value inside of forEach loop in JavaScript

I got stuck with my code.

I wrote this sample code only for the purpose to reproduce my problem, so this code is not practical at all but I hope you get my point.

In the code below, for the last value to be output, I expect it to be 3, but it's undefined.

How am I supposed to write if I want the last value to be 3 in this case??

This is just a sample code, but in the actual code, I fetch content from amazon api and when it returns api error, I want to run the same function again after 1000 milli seconds.

var list = [1,2,3];
var someClass = new SomeClass();

list.forEach(function(value) {

    var result = someClass.getSomething(value);

    console.log("outside: " + result);  

});


function SomeClass() {

    var flag = false;

    this.getSomething = function(something) {

        if (something === 3 && flag === false) {

            flag = true;
            this.getSomething(something);

            //I need to return here, so the succeeding code is not read.
            return;

        }

        console.log("inside: " + String(something));

        return something;

    }

}

Log

inside: 1
outside: 1
inside: 2
outside: 2
inside: 3
outside: undefined // I expect this value to be 3!!!

You have a test:

  if (something === 3 && flag === false) { //... return; 

If you want to return 3 then don't have a return statement with nothing after it. return means return undefined . Put return 3 or return something .

You probably want to return from your recursive call though:

return this.getSomething(something);

Here's the problem:

function SomeClass() {

    var flag = false;

    this.getSomething = function(something) {

        if (something === 3 && flag === false) {

            flag = true;

            this.getSomething(something); // not returning this

            return; // returning undefined

        }

        console.log("inside: " + String(something));

        return something;

    }

}

Here's the fix:

function SomeClass() {

    var flag = false;

    this.getSomething = function(something) {

        if (something === 3 && flag === false) {

            flag = true;

            return this.getSomething(something);

        }

        console.log("inside: " + String(something));

        return something;

    }

}

change the code to

if (something === 3 && flag === false) {

       flag = true;
        return this.getSomething(something);

}

"return;" returns undefined

It returns undefined in your code because you return; in the if clause.

return this.getSomething(something) within the if clause

var list = [1, 2, 3];
var someClass = new SomeClass();

list.forEach(function(value) {

    var result = someClass.getSomething(value);

    console.log("outside: " + result);

});


function SomeClass() {

    var flag = false;

    this.getSomething = function(something) {

        if (something === 3 && flag === false) {

            flag = true;
            return this.getSomething(something);

        }

        console.log("inside: " + String(something));

        return something;

    }

}

forEach loop doesn't return a value. You can create a global variable and assign values to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM