随机化javascript数组保持键值对

时间:2010-12-29 04:29:16

标签: javascript arrays random

我正在尝试在JavaScript中随机化数组,同时保持键/值对。这是我到目前为止所得到的:

data=[
["images/staff/img_staff_akwok_s.gif","Dr. A. Kwok","staff_akwok.phtml"],
["images/staff/img_staff_ayiu_s.gif","A. Yiu","staff_ayiu.phtml"],
["images/staff/img_staff_ally_s.gif","Ally Lee","staff_ally.phtml"],
["images/staff/img_staff_calvinkwan_s.gif","Calvin Kwan","staff_calvinkwan.phtml"],
["images/staff/img_staff_charles_s.gif","Charles Carlyle","staff_charles.phtml"],
["images/staff/img_staff_cherlotte_s.gif","Cherlotte","staff_cherlotte.phtml"],
["images/staff/img_staff_cychau_s.gif","C.Y. Chau","staff_cychau.phtml"],
["images/staff/img_staff_cyrus_s.gif","Cyrus","staff_cyrus.phtml"],
["images/staff/img_staff_cyruxleung_s.gif","梁詩穎","staff_cyruxleung.phtml"],
["images/staff/img_staff_davidchiu_s.gif","David Chiu","staff_davidchiu.phtml"],
["images/staff/img_staff_davidho_s.gif","David Ho","staff_davidho.phtml"],
["images/staff/img_staff_frankietam_s.gif","Frankie Tam","staff_frankietam.phtml"],
["images/staff/img_staff_jeremy_s.gif","Jeremy","staff_jeremy.phtml"],
["images/staff/img_staff_jng_s.gif","J.Ng","staff_jng.phtml"],
["images/staff/img_staff_jo_s.gif","Jo","staff_jo.phtml"],
["images/staff/img_staff_joesuen_s.gif","Joe Suen","staff_joesuen.phtml"],
["images/staff/img_staff_johnny_s.gif","張烈","staff_johnny.phtml"],
["images/staff/img_staff_jt_s.gif","J.T.","staff_jt.phtml"],
["images/staff/img_staff_jyeung_s.gif","J. Yeung","staff_jyeung.phtml"],
["images/staff/img_staff_kam_s.gif","曾定金","staff_kam.phtml"],
["images/staff/img_staff_kit_s.gif","黃政傑","staff_kit.phtml"],
["images/staff/img_staff_marcochan_s.gif","Marco Chan","staff_marcochan.phtml"],
["images/staff/img_staff_oten_s.gif","K.OTen","staff_oten.phtml"],
["images/staff/img_staff_leungsir_s.gif","梁頌佳","staff_leungsir.phtml"],
["images/staff/img_staff_lun_s.gif","李聚麐","staff_lun.phtml"],
["images/staff/img_staff_peterchow_s.gif","Peter Chow","staff_peterchow.phtml"],
["images/staff/img_staff_raphael_s.gif","Raphael","staff_raphael.phtml"],
["images/staff/img_staff_raymondwong_s.gif","Raymond Wong","staff_raymondwong.phtml"],
["images/staff/img_staff_rickyuen_s.gif","Rick","staff_rickyuen.phtml"],
["images/staff/img_staff_river_s.gif","余江河","staff_river.phtml"],
["images/staff/img_staff_samuelyu_s.gif","Samuel Yu","staff_samuelyu.phtml"],
["images/staff/img_staff_shun_s.gif","楊家舜","staff_shun.phtml"],
["images/staff/img_staff_stefanie_s.gif","Stefanie Chan","staff_stefanie.phtml"],
["images/staff/img_staff_thomas_s.gif","Thomas Yan","staff_thomas.phtml"],
["images/staff/img_staff_tonyyoon_s.gif","Tony Yoon","staff_tonyyoon.phtml"],
["images/staff/img_staff_victoria_s.gif","Victoria Poon","staff_victoria.phtml"],
["images/staff/img_staff_wchen_s.gif","陳偉亮 (W.Chen)","staff_wchen.phtml"]
]

data = data[Math.floor(Math.random()*data.length)]

2 个答案:

答案 0 :(得分:4)

这是关于shuffling algorithms的文章。

以下是从该文章中提取的一些Javascript代码:

var n = data.length;
for(var i = n - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var tmp = data[i];
    data[i] = data[j];
    data[j] = tmp;
}

答案 1 :(得分:0)

查看解决方案here

最短的是:

["a", "b", "c", "d"].sort(function() { return 0.5 - Math.random();});

然而,它并没有真正导致非常随机的顺序。