从阵列&中选择随机项.删除它,在数组为空后重新启动 [英] Select Random Item from Array & Remove It, Restart Once Array is Empty

查看:48
本文介绍了从阵列&中选择随机项.删除它,在数组为空后重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置从数组中选择一个随机项目.选择后,需要将其从阵列中删除,因此不会再次选择它.最后,清空阵列后,需要重新启动该过程.我正在尝试使用sessionStorage进行此操作,因为我需要跟踪选择了哪个随机项目.

I'm trying to set select a random item from an array. Once selected, it needs to be removed from the array so it does not get selected again. Finally, once the array is emptied, the process needs to restart. I'm trying to do this using sessionStorage because I need to keep track of which random item gets selected.

// Get array from sessionStorage
myArray = JSON.parse(sessionStorage.getItem("array"));

// If array does not exist in sessionStorage, set it
if (myArray === null) {
  sessionStorage.setItem("array", JSON.stringify(["apple", "orange", "banana"]));

// If array exists in sessionStorage, use it to get random item and empty it from array
} else {
  var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
  console.log(randomItem);
  console.log(myArray.splice(randomItem, 1));
}

可以在 此处 看到我的JSFiddle.

My JSFiddle can be seen here.

="a href =" https://jsfiddle.net/2jd9t14w/1/"rel =" nofollow>此处 .最终,该阵列被清除并重新启动.

Updated my work here. Eventually the array is cleared out and restarts.

推荐答案

这可能无法在此沙箱中运行(使用localstore),但我认为如果您尝试过,它应该可以工作.

This probably will not run in this sandbox (use of localstore), but I think it should work if you tried it.

// -------------------------------
// see: http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
// -------------------------------
function _shuffle (array) {
  for (var i = array.length - 1; i > 0; i--) {
      var j = Math.floor(Math.random() * (i + 1));
      var temp = array[i];
      array[i] = array[j];
      array[j] = temp;
  }
  return array;
}
// -------------------------------

// -------------------------------
// Get the next "random" item.
// -------------------------------
var randomItem  = (function(allItems){
  var _key = "array";
  var _currentItems = [];

  try {
    _currentItems = JSON.parse(localStorage.getItem(_key) || "[]");
  } catch (e) {
    _currentItems = [];
  }

  if (!Array.isArray(_currentItems) || _currentItems.length === 0 ) {
    console.log("resetting");
    _currentItems = _shuffle(allItems.slice());
  }

  var _selectedItem = _currentItems.pop();
  localStorage.setItem(_key, JSON.stringify(_currentItems));

  return _selectedItem;
})(["apple", "orange", "banana"]);
// -------------------------------

console.log(randomItem);

一个更裸露的版本[从上面带有_shuffle()]可能只是:

A more bare bones version [ with _shuffle() from above ] might be just:

var nextItem  = (function(all){
  var _key = "array";
  var _current = JSON.parse(localStorage.getItem(_key) || "[]");
  if (_current.length === 0) { _current = _shuffle(all.slice()); }

  var _selected = _current.pop();
  localStorage.setItem(_key, JSON.stringify(_current));

  return _selected;
})(["apple", "orange", "banana"]);

这篇关于从阵列&中选择随机项.删除它,在数组为空后重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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