'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>Iterating an array collection in Javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Iterating an array collection in Javascript - Stack Overflow

programmeradmin0浏览0评论

I'm having trouble with iterating and getting the values within a collection of arrays (an array of arrays I guess)

I'd hope that the code below would display an alert showing the 3 values of each array in turn (eg "infant", "0" and then "2") but the alert just displays "0" "undefined", undefined".

What am I missing?

Declare the array:

var ageGroups = [["infant", 0, 2], ["child", 3, 18], ["child", 3, 17], ["adult1", 18, 64], ["adult2", 65, 74], ["adult3", 75, 79]];

Iterate the array

for (var item in ageGroups) {
    alert(item[0]);
    alert(item[1]);
    alert(item[2]);
}

I'm having trouble with iterating and getting the values within a collection of arrays (an array of arrays I guess)

I'd hope that the code below would display an alert showing the 3 values of each array in turn (eg "infant", "0" and then "2") but the alert just displays "0" "undefined", undefined".

What am I missing?

Declare the array:

var ageGroups = [["infant", 0, 2], ["child", 3, 18], ["child", 3, 17], ["adult1", 18, 64], ["adult2", 65, 74], ["adult3", 75, 79]];

Iterate the array

for (var item in ageGroups) {
    alert(item[0]);
    alert(item[1]);
    alert(item[2]);
}
Share Improve this question asked Apr 4, 2012 at 12:31 BrightonDevBrightonDev 4352 gold badges7 silver badges21 bronze badges 1
  • anyway what you need to know is the for in loop returns item as a string , the name of the current property the loop is looking up , not an object or an array , so for in is not a foreach you can find in other langages , there is a forEach function for arrays in ES5 though – mpm Commented Apr 4, 2012 at 13:07
Add a ment  | 

7 Answers 7

Reset to default 4

use console.log instead of alert, Alert will show just [Object ], if variable is a object but in console you can see what kind of object and you can debug further

for (var item in ageGroups) { 
    console.log(ageGroups[item][0]); 
    console.log(ageGroups[item][1]); 
    console.log(ageGroups[item][2]); 
}
for (var item in ageGroups) {
    alert(ageGroups[item][0]);
    alert(ageGroups[item][1]);
    alert(ageGroups[item][2]);
}

your porblem is that item is the key of your array

try this:

for (var item in ageGroups) {
    alert(ageGroups[item][0]);
    alert(ageGroups[item][1]);
    alert(ageGroups[item][2]);
}

Use the damn forEach! :-) Not cross-browser though, but the shim is easy to implement.

// Call forEach and define the callback function
ageGroups.forEach(loopArray)

// Now let's work with the array!
function loopArray(ageGroup) {
    console.log(ageGroup[0])
    console.log(ageGroup[1])
    console.log(ageGroup[2])
}

You should do

for (i = 0; i <ageGroups.length; i++) {
    var item = ageGroups[i];
    alert(item[0]);
    alert(item[1]);
    alert(item[2]);
}

for..in in javascript is used to iterate over the properties of an object

Here's an optimized for loop, I'm storing the length here so it doesn't evaluate at every iteration:

 for (var i = 0, l = ageGroups.length; i < l; i++){
    alert(ageGroups[i][0]);
    alert(ageGroups[i][1]);  
    alert(ageGroups[i][2]);      
 }

to make it exactly like your example you can store the ageGroup's iteration in a variable:

for (var i = 0, l = ageGroups.length, item; i < l; i++){
    item = ageGroups[i];  
    alert(item[0]);
    alert(item[1]);
    alert(item[2]);
}

Don't use for in to iterate arrays in JavaScript. Its purpose is to iterate over object properties. Instead use an incremental for loop..

for (var i=0; i<ageGroups.length; i++) {
  for (var j=0; j<ageGroups[i].length; j++) {
    console.log(ageGroups[i][j]);
  }

  // Or instead of an inner loop access them individually
  console.log(ageGroups[i][0]);
  console.log(ageGroups[i][1]);
  console.log(ageGroups[i][2]);
}

See it in action...

Using the for-in construct on an array can produce drastically different results than an incremental loop if, for example, you defined only one array item like myArr[3] = 123;. In that case, JavaScript will allocate items 0-2, and the for loop will iterate them, but the for-in won't. More importantly, external scripts and frameworks may extend the Array prototype and add properties which will suddenly be included in your for-in iterator when you really just wanted the array elements.

发布评论

评论列表(0)

  1. 暂无评论