在jQuery JSON结果中ForEach数组值。

时间:2022-06-27 08:05:57

Okay, i want to process another javascript request foreach value returned inside a JSON response from a jQuery Request, Here's the current code i'm using for this request

好的,我想处理另一个javascript请求,foreach值从jQuery请求返回到JSON响应中,下面是我为这个请求使用的当前代码。

function waitForEvents(){
$.ajax({
            type: "GET",
            url: "/functions/ajax.php?func=feed&old_msg_id="+old_msg_id,

            async: true, /* If set to non-async, browser shows page as "Loading.."*/
            cache: false,
            timeout:50000, /* Timeout in ms */

            success: function(data){
                var json = jQuery.parseJSON(data);
                **//Foreach json repsonse['msg_id'] call another function**
                setTimeout('waitForEvents()',"1000");  
            },

            error: function (XMLHttpRequest, textStatus, errorThrown){
                alert("Error:" + textStatus + " (" + errorThrown + ")");
                setTimeout('waitForEvents()',"15000");       
            },
});
};

for each json response variable ['msg_id'] i want to call another javascript function but don't know how to process the array using a foreach in javascript, any idea how ?

对于每个json响应变量['msg_id'],我想调用另一个javascript函数,但不知道如何在javascript中使用foreach来处理这个数组,知道吗?

2 个解决方案

#1


2  

As you're already using jQuery, you can use the $.each function:

当您已经使用jQuery时,您可以使用$。每个函数:

http://api.jquery.com/jQuery.each/

http://api.jquery.com/jQuery.each/

$.each(json.msg_id, function (index, value) {
    // Do something with value here, e.g.
    alert('Value ' + index + ' is ' value);
})

#2


0  

use a simple for loop, likely faster than for each

使用一个简单的for循环,可能比每一个都快。

function myfunction(id){
    alert("myid:"+id):
}

var i=0;
for (i=0; i< json.length;i++){
    var thisId = json[i].msg_id;
    myfunction(thisId);
}

simpler:

简单:

function myfunction(id){
    alert("myid:"+id):
}

var i=0;
for (i=0; i< json.length;i++){
     myfunction(json[i].msg_id);
}

since you asked:

既然你问:

function checkArrayElements(element, index, array) {
     console.log("a[" + index + "] = " + element);
     var myId = element.msg_id;
};
json.forEach(checkArrayElements);

and discussion in case older browsers where not implimented: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach so you can do that

并讨论那些不受影响的旧浏览器:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach,这样您就可以做到这一点。

#1


2  

As you're already using jQuery, you can use the $.each function:

当您已经使用jQuery时,您可以使用$。每个函数:

http://api.jquery.com/jQuery.each/

http://api.jquery.com/jQuery.each/

$.each(json.msg_id, function (index, value) {
    // Do something with value here, e.g.
    alert('Value ' + index + ' is ' value);
})

#2


0  

use a simple for loop, likely faster than for each

使用一个简单的for循环,可能比每一个都快。

function myfunction(id){
    alert("myid:"+id):
}

var i=0;
for (i=0; i< json.length;i++){
    var thisId = json[i].msg_id;
    myfunction(thisId);
}

simpler:

简单:

function myfunction(id){
    alert("myid:"+id):
}

var i=0;
for (i=0; i< json.length;i++){
     myfunction(json[i].msg_id);
}

since you asked:

既然你问:

function checkArrayElements(element, index, array) {
     console.log("a[" + index + "] = " + element);
     var myId = element.msg_id;
};
json.forEach(checkArrayElements);

and discussion in case older browsers where not implimented: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach so you can do that

并讨论那些不受影响的旧浏览器:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach,这样您就可以做到这一点。