随机化javascript数组保持键值对

时间:2022-08-28 21:35:30

I'm trying to randomize an array in JavaScript whilst keeping key/value pairs. Here's what I've got so far:

我试图在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 个解决方案

#1


4  

Here's an article on shuffling algorithms.

这是一篇关于改组算法的文章。

And here's some Javascript code lifted from that article:

以下是从该文章中提取的一些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;
}

#2


0  

Look at the solutions here:

看看这里的解决方案:

The shortest is:

最短的是:

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

However, it does not really results in very random order.

但是,它并不会导致非常随机的顺序。

#1


4  

Here's an article on shuffling algorithms.

这是一篇关于改组算法的文章。

And here's some Javascript code lifted from that article:

以下是从该文章中提取的一些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;
}

#2


0  

Look at the solutions here:

看看这里的解决方案:

The shortest is:

最短的是:

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

However, it does not really results in very random order.

但是,它并不会导致非常随机的顺序。