Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript如何在运行多个IF语句后获取值_Javascript_Node.js_Function_If Statement_For Loop - Fatal编程技术网

Javascript如何在运行多个IF语句后获取值

Javascript如何在运行多个IF语句后获取值,javascript,node.js,function,if-statement,for-loop,Javascript,Node.js,Function,If Statement,For Loop,我有一个对象,我想循环遍历它,并针对一系列IF语句运行它。每个IF语句都发出一个HTTP请求。HTTP请求的结果被推送到一个数组。我需要在循环对象之后执行一个函数 如何仅在循环后以及语句完成后执行函数 代码: 我认为您仍然可以异步执行此操作。首先,您需要获取对象的所有属性: //Gather up all the properties; var properties = []; for(var property in object) if(object.hasOwnProperty(proper

我有一个
对象
,我想循环遍历它,并针对一系列IF语句运行它。每个IF语句都发出一个
HTTP请求
HTTP请求的结果
被推送到一个
数组
。我需要在循环对象之后执行一个函数

如何仅在循环后以及语句完成后执行函数

代码:


我认为您仍然可以异步执行此操作。首先,您需要获取对象的所有属性:

//Gather up all the properties;
var properties = [];
for(var property in object) if(object.hasOwnProperty(property) {
    properties.push(property);
}

//Array of results
var resultsArray = [];


//This is a named, self-invoked function that basically behaves like a loop. The
//loop counter is the argument "i", which is initially set to 0. The first if
//is basically the loop-termination condition. If i is greater than the
//total number of properties, we know that we're done.
//
//The success handler of the ajax call is where the loop is advanced. This is also
//where you can put your if condition to check if something is true, so that you
//can insert the data into the results array. At the end of the success handler,
//you basically call the named self-invoked function, but this time with the
//loop counter incremented. 
(function iterateOverProperties(i) {
    if(i < properties.length) {
        var property = properties[i];
        var value = object[property];

        //assuming jQuery just for demonstration
        jQuery.ajax({
            url: "someurl",
            data: value;
            success: function(data) {
                if(something is true) {
                    resultArray.push(data);
                }

                iterateOverProperties(++i);
            }
        });
    } else {
        res(resultArray);
    }
})(0);
//收集所有属性;
var属性=[];
for(对象中的var属性)if(object.hasOwnProperty(属性){
属性。推送(属性);
}
//结果数组
var结果数组=[];
//这是一个命名的自调用函数,其行为基本上类似于循环
//循环计数器是参数“i”,它最初设置为0
//基本上是循环终止条件。如果i大于
//属性的总数,我们知道我们已经完成了。
//
//ajax调用的成功处理程序是循环的高级处理程序
//你可以把你的if条件放在那里,检查事情是否属实,这样你就可以
//可以将数据插入结果数组。在成功处理程序结束时,
//您基本上调用了命名的自调用函数,但这次使用
//循环计数器递增。
(函数iterateOverProperties(i){
if(i
这将起作用,这取决于你的
做什么
如果它做动画或者
get
s不太可能起作用,那么这将起作用。另外,不要使用
对象
作为变量,这是一个保留字,非常多。如果你
做什么
是通过ajax发出这些HTTP请求的,请确保你将通过ajax执行这些请求n同步。换句话说,在执行
res(resultaray)之前,请确保收到响应
。还要记住,HTTP请求很昂贵,在一个HTTP请求中将对象发送到服务器,然后返回生成的数组会更有效吗?@Blowsie是的,我的示例中的变量名没有被使用。这不起作用,因为我正在发出HTTP请求,它会在我得到响应之前移动。@Christopher.Cubells我的每个IF语句都向不同的端点发出不同的http请求。它很可能会在我需要捕获的For循环中发出2-3个请求。在这种情况下,我可以同步进行调用,但我该如何做呢?如果我将它们传递给一个新函数,a)该函数如何知道它们可能会更频繁地出现?b)我想我将失去进行回调的访问权。@Rob:jQuery的ajax函数有一个名为
async
的参数,只需将其设置为false即可。例如:
$.ajax({url:/getdata],async:false,success:function(){})
//Gather up all the properties;
var properties = [];
for(var property in object) if(object.hasOwnProperty(property) {
    properties.push(property);
}

//Array of results
var resultsArray = [];


//This is a named, self-invoked function that basically behaves like a loop. The
//loop counter is the argument "i", which is initially set to 0. The first if
//is basically the loop-termination condition. If i is greater than the
//total number of properties, we know that we're done.
//
//The success handler of the ajax call is where the loop is advanced. This is also
//where you can put your if condition to check if something is true, so that you
//can insert the data into the results array. At the end of the success handler,
//you basically call the named self-invoked function, but this time with the
//loop counter incremented. 
(function iterateOverProperties(i) {
    if(i < properties.length) {
        var property = properties[i];
        var value = object[property];

        //assuming jQuery just for demonstration
        jQuery.ajax({
            url: "someurl",
            data: value;
            success: function(data) {
                if(something is true) {
                    resultArray.push(data);
                }

                iterateOverProperties(++i);
            }
        });
    } else {
        res(resultArray);
    }
})(0);