0
我有一個包含10個元素的數組,我在窗口加載的其他數組中選取了其中的隨機6個元素。我不想保持新數組中任何元素的位置與頁面刷新時的前一個元素相同。我應該怎麼做?檢查randomize後數組元素的位置是否相同
var tempArray = [1,2,3,4,5,6,7,8,9,10]; //inital array with 10 elements
var newArray = []; //array where random 6 elements will be pushed from tempArray
//randomize function
function randomNoRepeats(array) {
var copy = array.slice(0);
return function() {
if (copy.length < 1) { copy = array.slice(0); }
var index = Math.floor(Math.random() * copy.length);
var item = copy[index];
copy.splice(index, 1);
return item;
};
};
var chooser = randomNoRepeats(tempArray);
for(var i=1; i<7; i++){
newArray.push(chooser());
}
newArray = [4,7,9,2,10,1] //Suppose I get this as newArray
newArray = [2,1,5,8,10,3] //On refresh suppose 10 occurs at the same position as previous. Need to avoid this.
在此先感謝您的幫助。
拿什麼*窗口負載*?您需要在某處存儲以前的值。 –
由_window load_我的意思是在每一頁刷新。更正它。 –