简体   繁体   English

如何在JavaScript中的forEach循环内返回值

[英]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. 在下面的代码中,要输出最后一个值,我希望它是3,但未定义。

How am I supposed to write if I want the last value to be 3 in this case?? 在这种情况下,如果我希望最后一个值为3,我应该怎么写?

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. 这只是一个示例代码,但是在实际代码中,我从Amazon api获取内容,当它返回api错误时,我想在1000毫秒后再次运行相同的函数。

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. 如果要返回3则后面没有任何return语句。 return means return undefined . return表示return undefined Put return 3 or return something . return 3return 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) { 如果(某物=== 3 &&标志===否){

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

} }

"return;" “返回;” returns undefined 返回未定义

It returns undefined in your code because you return; 它在您的代码中返回undefined ,因为您return; in the if clause. if子句中。

return this.getSomething(something) within the if clause if子句中return this.getSomething(something)

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. forEach循环不返回值。 You can create a global variable and assign values to it. 您可以创建一个全局变量并为其分配值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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