对象数组上的Javascript Foreach循环?
我目前正在使用Ajax,使用API从应用程序中反复提取控制台日志。在PHP中执行返回值的var_dump()时,它是一个对象数组,我需要循环并提取这些值 这在PHP中当然很简单,但由于我对Javascript缺乏经验,我无法用for或foreach循环解决这个问题。我已经将console.log与Developer console一起使用,内容也在那里,但是如果您能提供有关如何循环的帮助,我们将不胜感激 JS/Ajax:对象数组上的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
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”]我试过了,但没有成功,“结果”什么都不是。我刚刚更新了我的OP以显示发送响应的php代码,您可以看到“message”是我放置结果内容的地方。对此很抱歉,但是如果这是发送ajax响应的php处理程序,那么您将在对象内部发送消息
,您应该能够通过以下内容接收它:var message=data[“message”]; 控制台日志(消息)代码>如何实际循环每个值?我想自己手动记录每个“源”和“内容”值,这就是问题所在。谢谢你迄今为止的帮助!好的,我想如果数组在php处理程序发送的消息变量中,我会更了解您的代码段,然后尝试以下操作:var messages=data['message']['result']代码>