Javascript/JSP随机排序两个相等字符串数组
我在脚本中有一个字符串数组,我对它进行随机排序,然后使用它。然后我想用url将相同的排序数组传递给Jsp页面,但这很困难,所以我想在脚本和Jsp页面中生成相同的随机数组,您能帮我吗Javascript/JSP随机排序两个相等字符串数组,javascript,arrays,jsp,sorting,Javascript,Arrays,Jsp,Sorting,我在脚本中有一个字符串数组,我对它进行随机排序,然后使用它。然后我想用url将相同的排序数组传递给Jsp页面,但这很困难,所以我想在脚本和Jsp页面中生成相同的随机数组,您能帮我吗 var shuffle = ['0B', '1B', '2B', '3B', '4B', '5B', '6B', '7B', '8B', '9B', '0C', '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '0O', '1O', '2O', '3O',
var shuffle = ['0B', '1B', '2B', '3B', '4B', '5B', '6B', '7B', '8B', '9B', '0C', '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '0O', '1O', '2O', '3O', '4O', '5O', '6O', '7O', '8O', '9O', '0S', '1S', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S'];
我怎样才能在所有需要的时候对shuffle进行平均排序 这就是你要找的吗? 我做了两个文件。一个生成洗牌数组,然后编码URL的程序;读取该URL的一个设置数组的顺序 encode.php
<div id="click" onclick="encode()">Click</div>
<div id="log"></div>
<script>
// I presume both files have the same array
var items = ['hello', 'world', 'foo', 'bar'];
function encode() {
var data = shuffle_and_encode_order(items);
// notice: data.keys is the array containing the keys; data.values contains the values
// we encode the order to a URL readable format
var url = '#' + data.keys.toString() +'';
document.getElementById('log').innerHTML =
data.values.toString() + '<br>' +
'<a href="decode.php' + url + '">LINK for this order</a>';
}
// encode the order of the array (to url)
function shuffle_and_encode_order (items) {
// retreive the keys of the array. So this returns [0, 1, 2, ...]
var keys = Object.keys(items);
// now we sort 'keys', and remember the order of that array
var keys_sorted = shuffle(keys);
var result = {keys: [], values: []};
// we fill var result with the items; in the new order
for (var i=0; i<items.length; i++) {
result.keys.push(keys_sorted[i]);
result.values.push(items[keys_sorted[i]]);
}
return result;
}
// @see http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array#2450976
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
</script>
点击
//我假定两个文件具有相同的数组
var items=['hello','world','foo','bar'];
函数encode(){
var数据=无序排列和编码顺序(项目);
//注意:data.keys是包含键的数组;data.values包含值
//我们将订单编码为URL可读格式
var url='#'+data.keys.toString()+'';
document.getElementById('log')。innerHTML=
data.values.toString()+'
'+
'';
}
//编码数组的顺序(到url)
函数无序排列和编码顺序(项目){
//检索数组的键。因此返回[0,1,2,…]
变量键=对象键(项);
//现在我们对“键”进行排序,并记住该数组的顺序
var keys_sorted=shuffle(键);
var result={keys:[],value:[]};
//我们用项目填充var结果;以新的顺序
对于(var i=0;ii)如果我理解这一点,您就不会有混乱项目的问题。您希望保存项目的顺序(键的顺序),并且您希望能够在url中读取/写入该顺序。这是可以做到的
<div id="log"></div>
<script>
// I presume both files have the same array
var items = ['hello', 'world', 'foo', 'bar'];
function decode() {
// we read the url, everything to the right of the # symbol.
var hash = window.location.hash.substr(1); // that substr is to remove the #.
var keys = hash.split(','); // makes an array of this string; separates the items by comma characters
var values = items_set_order(items, keys);
document.getElementById('log').innerHTML =
values.toString() + '<br>';
}
function items_set_order(items, keys) {
var result = [];
for (var i=0; i<items.length; i++) {
result.push(items[keys[i]]);
}
return result;
}
window.onload = decode();
</script>