javascript中的计数数组 [英] Count array in javascript
问题描述
大家好,我想问一下您如何计算数组的大小?我有一个数组,该数组从我的php文件传递的json_encode值中获取其值...这是我的php返回json数据的格式
Hi guys i would like to ask how do you count the size of an array? i have an array that gets its value from the json_encode value passed by my php file... here is the format of my php return json data
<?php
$idArr= array();
while($row= mysql_fetch_array($outputArrayResult)){
$idArr["" . $row[''id'']] = false;
}
echo json_encode(array("total_pages" => $num_pages,"ids" => $idArr));
?>
它以这种格式返回
which returns in this format
/* firebug output */
{
"total_pages":12,
"ids":{"1":false,"2":false,"3":false,"4":false,"5":false,"6":false,"7":false,"8":false,"9":false,"10":false,"11":false,"12":false}
}
现在在我的JavaScript中,我处理使用此代码返回的ajax数据
now in my javascript i handle the ajax data returned using this code
/* ajax request success handler */
success: function(data){
var obj = jQuery.parseJSON(data);
var ids= obj.ids;
alert("this is " + ids.length + " -- " + ids["2"]);
}
如您所见,我使用jQuery.parseJSON(data)检索数据并将其传递给我的变量ID.现在,当我尝试提醒该值时,它将为我提供此输出
as you can see im retrieving the data using jQuery.parseJSON(data) and pass it to my variable ids. now when i try to alert the value it gives me this output
/* alert message */
this is undefined -- false
因此,这意味着它成功检索了ids ["2"]的值,但是我无法获取要正确输出的数组的长度...您如何做到这一点?
so it means it successfully retrieved the value of ids["2"] but i cant get the length of the array to output properly... how do you do this?
推荐答案
idArr = array(); while(
idArr= array(); while(
row = mysql_fetch_array(
row= mysql_fetch_array(
outputArrayResult)){
outputArrayResult)){
这篇关于javascript中的计数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!