javascript - 防止在数组中添加相同的随机值

标签 javascript arrays react-native

我正在制作一个功能,其中使用 for 循环将随机索引添加到数组中,值必须始终不同,但不知何故有时(大约每 10 个函数调用)存在相同的值,即使我试图过滤相同的值来自 answerId-array 并放置其余值 unique-array 并检查已过滤的 unique-array 和原始 answerId-array 具有相同的长度,如果 unique-arrays 长度小于 unique-arrays 长度,answerId的值将被更改。但即使 answerId 数组中有相同的值,这些数组也始终具有相同的长度,并且运行无法传递到其余的 for 循环(这些 for 循环是相当一些黑客代码)。我做错了什么?抱歉,如果我的英语水平不好。

getJSONData(){

  var answerId = [null, null, null, null];
  var length = Object.keys(Company.person).length;

  for(var i = 0; i <= answerId.length - 1; i++){
    answerId[i] = [Math.floor(Math.random() * length)]

  }

  let unique = Array.from(new Set(answerId))
  console.log(unique.length)

  if (unique.length < answerId.length){
    for(var i = 0; i <= answerId.length - 1; i++){
      answerId[i] = [Math.floor(Math.random() * length)]
    }
    console.log("new values 1")
    unique = Array.from(new Set(answerId))

    if (unique.length < answerId.length){
      for(var i = 0; i <= answerId.length - 1; i++){
        answerId[i] = [Math.floor(Math.random() * length)]
      }
      console.log("new values 2")
      unique = Array.from(new Set(answerId))

      if (unique.length < answerId.length){
        for(var i = 0; i <= answerId.length - 1; i++){
          answerId[i] = [Math.floor(Math.random() * length)]
        }
        console.log("new values 3")
        unique = Array.from(new Set(answerId))

        if (unique.length < answerId.length){
          for(var i = 0; i <= answerId.length - 1; i++){
            answerId[i] = [Math.floor(Math.random() * length)]
          }
          console.log("new values 4")
        }
      }
    }
  }

  var personArray = [Company.person[answerId[0]].firstName + ' ' + Company.person[answerId[0]].lastName, Company.person[answerId[1]].firstName + ' ' + Company.person[answerId[1]].lastName, Company.person[answerId[2]].firstName + ' ' + Company.person[answerId[2]].lastName, Company.person[answerId[3]].firstName + ' ' + Company.person[answerId[3]].lastName];
  return personArray;
}

最佳答案

您可以直接使用Setsize用于检查结果集所需大小的属性。

var array = [0, 1, 42, 17, 22, 3, 7, 9, 15, 35, 20],
    unique = new Set;

while (unique.size < 4) {
    unique.add(Math.floor(Math.random() * array.length));
}

console.log([...unique]); // indices

否则,您可以使用哈希表。

var array = [0, 1, 42, 17, 22, 3, 7, 9, 15, 35, 20],
    unique = {},
    id = [],
    i;

while (id.length < 4) {
    i = Math.floor(Math.random() * array.length);
    if (!unique[i]) {
        id.push(i);
        unique[i] = true;
    }
}

console.log(id);

关于javascript - 防止在数组中添加相同的随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41716168/

相关文章:

python - 在 python 中追加 2d 时间序列数组

c++ - 如何对对应于其他数组元素的数组元素进行排序?

ios - 在 React Native 前台设置 PushNotificationIOS 时发出警告

ios - react native iOS : Could not build module 'yoga' : 'algorithm' file not found

javascript - Angular Js...,ng-click 功能不起作用

javascript - 简化我的 JQuery 困惑

javascript - jQuery-ui 的调整大小功能无法与 chrome 中的 flexbox 模型正确交互,但在 FF 和 IE 中成功

javascript - 在 AngularJS 中,在模板内的过滤器中使用 $

java - 如何在java中将Time转换为int

react-native - 用多行 react 原生水平 FlatList