【问题标题】:node.js | Inserting elements from one array into random locations within another array节点.js |将一个数组中的元素插入另一个数组中的随机位置
【发布时间】:2019-01-21 06:59:13
【问题描述】:

假设我有两个数组:

let arr1 = [1,2,3,4,5] ;
let arr2 = ['a' , 'b', 'c'] ;

我想在 arr1 中随机插入 arr2 的元素。 arr2 的顺序并不重要,但 arr1 的顺序很重要。目标是得到这样的结果:

let mergedArray = [1 , 2, 'b', 3 , 'c',4, 'a',5] ;

我怎样才能做到这一点?

【问题讨论】:

    标签: javascript arrays node.js


    【解决方案1】:

    使用我的答案 here 中的函数,我们可以对两个数组使用 shuffle 函数,我们将其与 concat 合并:

    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex;
    
      // While there remain elements to shuffle...
      while (0 !== currentIndex) {
    
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
    
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }
    
      return array;
    }
    
    let arr1 = [1, 2, 3, 4, 5];
    let arr2 = ["a", "b", "c"];
    
    let mergedArray = shuffle(arr1.concat(arr2));
    
    console.log(mergedArray);

    【讨论】:

      【解决方案2】:
      let arr1 = [1,2,3,4,5] ;
      let arr2 = ['a' , 'b', 'c'] ;
      
      var len1 = arr1.length
      var mergedArr = arr1.slice()
      
      for(var i = 0; i< arr2.length; i++){
        var rand = Math.floor(Math.random() * (len1 - 0)) + 1
        mergedArr.splice(rand, 0, arr2[i])
      }
      console.log(mergedArr)
      

      【讨论】:

        【解决方案3】:

        我的简单方法:混合 arr2 然后将 arr2 的每一项插入到 arr1 中随机化

        function getRandomInt(min, max) {
          min = Math.ceil(min);
          max = Math.floor(max);
          return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        
        function shuffleArray(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;
          }
        }
        
        function shuffle(arr1, arr2) {
          // Randomize arr2 element order in-place.
          shuffleArray(arr2);
        
        
          arr2.forEach(item => {
            // Randomize index of arr1
            let i = getRandomInt(0, arr1.length);
            // Insert item to  arr1
            arr1.splice(i, 0, item);
          });
          return arr1;
        }
        
        let arr1 = [1, 2, 3, 4, 5];
        let arr2 = ["a", "b", "c"];
        
        let result = shuffle(arr1.concat(arr2));
        
        console.log(result);
        

        【讨论】:

          【解决方案4】:

          这对你有帮助

          let arr1 = [1,2,3,4,5] ;
          let arr2 = ['a' , 'b', 'c'] ;
          
          
           arr2.map((values,i)=>{
             var rand = Math.floor(Math.random() * (arr1.length)) + 1;
               arr1.splice(rand, 0,values);
          
           })
           console.log(arr1)
          

          【讨论】:

            【解决方案5】:
            var one=[1,2,3,4];
                var two=['a','b','c'];
                var three=one.concat(two);
                var four=[];
            var test=three.length;
                for(i=0;i<test;i++){
                     let j=Math.floor((Math.random() * three.length));
                        four.splice(j, 0,three[i]);
                }
            console.log(four);
            

            【讨论】:

            • 此解决方案生成错误结果。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-21
            • 1970-01-01
            • 2011-07-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多