Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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 Foreach循环?_Javascript_Php_Arrays_For Loop_Foreach - Fatal编程技术网

对象数组上的Javascript Foreach循环?

对象数组上的Javascript Foreach循环?,javascript,php,arrays,for-loop,foreach,Javascript,Php,Arrays,For Loop,Foreach,我目前正在使用Ajax,使用API从应用程序中反复提取控制台日志。在PHP中执行返回值的var_dump()时,它是一个对象数组,我需要循环并提取这些值 这在PHP中当然很简单,但由于我对Javascript缺乏经验,我无法用for或foreach循环解决这个问题。我已经将console.log与Developer console一起使用,内容也在那里,但是如果您能提供有关如何循环的帮助,我们将不胜感激 JS/Ajax: function getConsoleMessages() { v

我目前正在使用Ajax,使用API从应用程序中反复提取控制台日志。在PHP中执行返回值的var_dump()时,它是一个对象数组,我需要循环并提取这些值

这在PHP中当然很简单,但由于我对Javascript缺乏经验,我无法用for或foreach循环解决这个问题。我已经将console.log与Developer console一起使用,内容也在那里,但是如果您能提供有关如何循环的帮助,我们将不胜感激

JS/Ajax:

function getConsoleMessages()
{
    var messageBox = document.getElementById("console_message");

    // Clear the message box contents
    //messageBox.value = '';

    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: { 'action': 'getConsoleMessages' },
        dataType: 'json',
        success: function(data)
        {
            var messages = data['message'];

            messages.forEach( function (item)
            {
                var x = item.Contents;
                console.log(x);
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        { 
            alert("Status: " + textStatus); alert("Error: " + errorThrown); alert("Message: " + XMLHttpRequest.responseText);
        }
    });
};
PHP处理程序:

case "sendConsoleMessage":
{
    $message = $_POST["message"];

    if (empty($message))
    {
        $response['status'] = "failed";
        $response['message'] = "Command parameter was not received.";
    }
    else
    {
        $amp->sendConsoleMessage($message);

        $response['status'] = "success";
        $response['message'] = $message;
    }

    echo json_encode($response);

    break;
}
PHP变量转储:

object(stdClass)[2]
  public 'result' => 
    array (size=40)
      0 => 
        object(stdClass)[3]
          public 'Timestamp' => string '/Date(1422419818830-0500)/' (length=26)
          public 'Source' => string 'Console' (length=7)
          public 'Type' => string 'Console' (length=7)
          public 'Contents' => string 'Assigned anonymous gameserver Steam ID [A:1:721403909:5132].' (length=60)
      1 => 
        object(stdClass)[4]
          public 'Timestamp' => string '/Date(1422419819038-0500)/' (length=26)
          public 'Source' => string 'Console' (length=7)
          public 'Type' => string 'Console' (length=7)
          public 'Contents' => string 'VAC secure mode is activated.' (length=29)
      2 => 
        object(stdClass)[5]
          public 'Timestamp' => string '/Date(1422419819145-0500)/' (length=26)
          public 'Source' => string 'Console' (length=7)
          public 'Type' => string 'Console' (length=7)
          public 'Contents' => string 'tf_server_identity_account_id not set; not logging into registered account' (length=74)
从你的数据来看, 我想你想做标准的

for(i;i<count;i++)
{

   //then and this is the part I believe you are missing:
    for (variable in object) 
    {  str += variable + ":" + object[variable] ;  }

}
用于(i;i语法:

for ([start]; [condition]; [final-expression])
    statement
规则:

Traditional way of iterating over arrays.
Can use var, but scope is always the complete surrounding function.
例如:

var arr = [ "a", "b", "c" ];
for(var i=0; i < arr.length; i++) {
    console.log(arr[i]);
}
var arr=[“a”、“b”、“c”];
对于(变量i=0;i
试试这个:

function getConsoleMessages()
{
    var messageBox = document.getElementById("console_message");

    // Clear the message box contents
    //messageBox.value = '';

    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: { 'action': 'getConsoleMessages' },
        dataType: 'json',
        success: function(data)
        {
            var messages = data['message']['result'];

            messages.forEach( function (item)
            {
                var x = item.Contents;
                console.log(x);
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        { 
            alert("Status: " + textStatus); alert("Error: " + errorThrown); alert("Message: " + XMLHttpRequest.responseText);
        }
    });
};

我使用console.log将返回的值打印到开发者控制台,并将其添加到原始消息中,但是它似乎丢失了格式:/from MDN我刚刚用我的函数更新了原始帖子,并使用了数组forEach(),但我刚刚在var x=item.Contents上得到了“undefined is not a function”;谢谢:),检查我的答案,我想这会有帮助。你只是从中复制答案,没有任何解释。这行:
var messages=data[“result”]消息
,您应该能够通过以下内容接收它:
var message=data[“message”]; 控制台日志(消息)如何实际循环每个值?我想自己手动记录每个“源”和“内容”值,这就是问题所在。谢谢你迄今为止的帮助!好的,我想如果数组在php处理程序发送的消息变量中,我会更了解您的代码段,然后尝试以下操作:
var messages=data['message']['result']