循环遍历关联数组 [英] Loop through associative array in reverse

查看:172
本文介绍了循环遍历关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript关联数组(arr)并使用此方法循环遍历它。

I'm using a javascript associative array (arr) and am using this method to loop through it.

for(var i in arr) {
    var value = arr[i];
    alert(i =") "+ value);
}

问题是项目的顺序对我很重要,它需要从最后到第一个循环,而不是像现在一样循环到第一个。

The problem is that the order of the items is important to me, and it needs to loop through from last to first, rather than first to last as it currently does.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

使用按相反顺序按住键的临时数组:

Using a temporary array holding the keys in reverse order:

var keys = new Array();

for (var k in arr) {
    keys.unshift(k);
}

for (var c = keys.length, n = 0; n < c; n++) {
   alert(arr[keys[n]]);
}

这篇关于循环遍历关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆