代码之家  ›  专栏  ›  技术社区  ›  Salar

节点。js |将一个数组中的元素插入另一个数组中的随机位置

  •  1
  • Salar  · 技术社区  · 7 年前

    假设我有两个数组:

    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] ;
    

    我怎样才能做到这一点?

    5 回复  |  直到 7 年前
        1
  •  2
  •   Jack Bashford    7 年前

    使用我的答案中的函数 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
  •  2
  •   Candy    7 年前
    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
  •  1
  •   hoangdv    7 年前

    我的简单方法是:混合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
  •  1
  •   vinoth s    7 年前

    这对你有帮助

    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
  •  1
  •   renish    7 年前
    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);
    
    推荐文章