【问题标题】:how to change the value of a random value within a javascript array?如何更改javascript数组中随机值的值?
【发布时间】:2020-10-11 11:08:40
【问题描述】:

我在一个函数中接收到一个数组,其中包含已定义的值和要定义的其他值。


["y", null, null, null, null, null, null, null, null]

我想要做的是,每次收到这个数组时,我都会检查为 null 的值,并检查其中一个以随机方式选择的值,我想设置值“Z”并返回数组修改。

为此,我在我的函数中执行以下操作:

    nextValue(array) {
        
        const random = Math.floor(Math.random() * array.length);        
    }
};

此时,我在其他参考资料中找到了,如果我打印值“random = Nan” 和“array[random] = undefined”。

如何修改值?

有没有更好的方法,不需要提取值,直接修改?

【问题讨论】:

    标签: javascript arrays random


    【解决方案1】:

    真的不清楚你到底想要什么。假设任务是“获取一个包含 null 和其他一些值的元素的数组,将一个随机的 null 翻转到某个预设字符串,就地执行此操作”,您可能会使用以下内容:

    function flipRandomNull(arrayWithNulls, character) {
      const indexesOfNulls = arrayWithNulls.reduce((acc, item, index) => {
        return item === null ? [...acc, index] : acc;
      }, []);
    
      if (!indexesOfNulls.length) return; // no nulls for ya, nothing to flip
    
      const indexToFlip = indexesOfNulls[ Math.random() * indexesOfNulls.length | 0];
      arrayWithNulls[ indexToFlip ] = character;
    }
    
    const initialField = ["y", null, null, null, null, null, null, null, null];
    
    [...Array(initialField.length - 1)].forEach(() => {
      flipRandomNull(initialField, 'Z');
      console.log(initialField.join(' | '));
    });

    【讨论】:

      【解决方案2】:

      您可以使用 array.map 和 array.filter 方法找出数组中空项的索引,然后用“z”填充主数组的随机位置:

      const arr = ["y", null, null, null, null, null, null, null, null];
      
      const nextValue = (orgArr) => {
          const nullLocations = orgArr
              .map((item, idx) => (item === null ? idx : null))
              .filter((item) => item !== null);
          orgArr[nullLocations[Math.floor(Math.random() * nullLocations.length)]] = "z";
          return orgArr;
      };
      
      console.log(nextValue(arr));
      

      【讨论】:

        【解决方案3】:

        这是我的看法...

        let arr = ["y", null, null, null, null, "y", null, null, null];
        let size = arr.length;
        // size of array
        
        function random() {
          var random = Math.floor(Math.random() * size + 1)
          //generate radnom number betwen 0 and size of array
          if (arr[random] === null) {
            arr[random] = "z";
            // if random array  is null set z
            return size = 0;
            //stop loop
          } else {
            random = Math.floor(Math.random() * size + 1);
            // if not set new radnom number
          }
        }
        
        var i = 0;
        while (i < size) {
          random()
          i++;
        }
        // loop function untill it sets radnom array null to z
        console.log(arr)

        加上更短的功能:

        function random() {
          var random = Math.floor(Math.random() * size + 1)
          arr[random] === null ? (arr[random] = "z", size = 0 ) : (random = Math.floor(Math.random() * size + 1));  
        }
        

        let arr = ["y", null, "y", "y", null, "y", null, null, null];
        let size = arr.length;
        
        
        var i = 0;
        while (i < size) {
          random()
          i++;
        }
        
        function random() {
          var random = Math.floor(Math.random() * size + 1)
          arr[random] === null ? (arr[random] = "z", size = 0, console.log("set, " + (i + 1) + " trys")) : (random = Math.floor(Math.random() * size + 1));
        }
        
        
        console.log(arr)
        .as-console-wrapper {
          min-height: 100%
        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-07-11
          • 1970-01-01
          • 2018-06-19
          • 2022-10-08
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 2017-02-20
          相关资源
          最近更新 更多