Javascript - 随机选择对象中的项目,但不选择已排除的项目

时间:2017-09-14 16:15:51

标签: javascript object random

我正在开发一个项目,我需要在一个对象中包含很多函数,以便我可以使用以不同方式组合访问不同方法的变量来访问它们。我还没有在我的主项目中写这个,只是将其作为一种方法进行测试,我将下面的测试代码放在希望更好地解释我在说什么。此对象中将有许多功能,目的是在按下按钮时随机运行其中一个。

我想要做的一个关键部分是用户可以排除他们想要随机选择的任何功能,这就是导致我出现问题的原因。我读了很多其他问题的解决方案,但所有这些都只是一点点(或者至少我不知道如何将它们应用于此)。我尝试的第一件事就是使用一个数组,取决于用户选择的选项,排除或添加项目,然后从中随机选择以获取随机数,但我担心大约300个项目的数组只会减慢速度或造成更多问题,而不是真正有用,所以我放弃了这一点。我查找了如何从随机代中排除数字,但这往往用于循环,只适用于少数数字被排除 - 之后我想这也会引起问题。我考虑从随机生成中排除数字范围,但是用户排除的项目不能保证在对象中彼此相邻,因此会导致问题。

我头脑中的计划是在对象键的末尾添加一个数字;随机数生成器将从所有未排除的数字中选择(基于密钥的其他部分排除,例如在下面的测试中排除所有'沙拉'选项),然后是该数字,以及两个或三个其他变量弥补关键带给我们一个真实的东西。因此,在下面的主题中,可以根据用户选择的选项和随机数分别选择“培根”+“汉堡”+“生菜”+“17”,并可能导致基于第17次培根生菜的警报 - 我“顺便说一下,这不是制作某种食物大喊大叫的应用程序,而是真的只是一个演示...

这是......希望它听起来不那么复杂。我想我已经解释得非常好了,所以如果我能澄清一些东西请告诉我。或者,如果为此目的在对象内嵌套函数是直接愚蠢的,请告诉我!我想学习最好的做事方式,而不是摸索丑陋和不雅的东西!

var food;
var type;
var func = {
  cheeseburger: function() {alert("This is CHEESE BURGER");},
  baconburger: function() {alert("BACON BURGER reigns supreme!");},
  cheesesalad: function() {alert("CHEESE SALAD hahahaha");},
  baconsalad: function() {alert("BACON SALAD! That's right!");}
 };

$(".cheese").click(function() {
  food = "cheese";
  $(".test").html(food);
});

$(".bacon").click(function() {
  food = "bacon";
  $(".test").html(food);
});

$(".burger").click(function() {
  type = "burger";
  $(".test2").html(type);
});

$(".salad").click(function() {
  type = "salad";
  $(".test2").html(type);
});

$(".go").click(function() {
  func [food + type]();
});

2 个答案:

答案 0 :(得分:0)

const funcs = [
   v => alert(v),
   v => console.log(v),
   v => alert( v + 1)
];

const excluded = new Set();

//to exclude a function
excluded.add(2);

function random(...args){
  do {
    var i = Math.floor( Math.random() * funcs.length );
  } while( excluded.has( i ) );
  funcs[i](...args);
}

 //test it
 random("test");

答案 1 :(得分:0)

首先,您需要一些属性来启用或禁用功能。

<div class="wrapper">
  <div class="nested-wrapper1">
    <div class="1">
      1. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
  </div>
  <div class="nested-wrapper2">
    <div class="2">
      2. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
       2. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    </div>
    <div class="3">
      3. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
  </div>
</div>

然后选择一个忽略禁用的随机函数。

这里出现了第一个问题:如果您只是随机选择一个,直到找到一个 没有禁用,因为残疾人的数量增加,它将需要 更长时间才能找到启用的。

更简单的解决方案是使用如下的缓存:

var func = {
    cheeseburger: {f: function() {...}, enabled: true},
    baconburger: {f: function() {...}, enabled: true},
    cheesesalad: {f: function() {...}, enabled: true},
    baconsalad: {f: function() {...}, enabled: true},
  ...
};

然后你也需要启用/禁用方法:

var cache;
function updateCache(){ // Using function we can update it when needed.
    cache = Object.keys(func).filter(k=>func[k].enabled);
};
updateCache();

最后,您只需要从缓存中选择功能键:

function enable(fn) {
    func[fn].enabled = true;
    updateCache();
};
function disable(fn) {
    func[fn].enabled = false;
    updateCache();
};