Rotate the elements in an array in JavaScript

后端 未结 30 1196
走了就别回头了
走了就别回头了 2020-11-22 10:55

I was wondering what was the most efficient way to rotate a JavaScript array.

I came up with this solution, where a positive n rotates the array to the

相关标签:
30条回答
  • 2020-11-22 11:04

    see http://jsperf.com/js-rotate-array/8

    function reverse(a, from, to) {
      --from;
      while (++from < --to) {
        var tmp = a[from];
        a[from] = a[to];
        a[to] = tmp;
      }
    }
    
    function rotate(a, from, to, k) {
      var n = to - from;
      k = (k % n + n) % n;
      if (k > 0) {
        reverse(a, from, from + k);
        reverse(a, from + k, to);
        reverse(a, from, to);
      }
    }
    
    0 讨论(0)
  • Not sure about the efficiency but I would do it in this non mutating way :

    	Array.prototype.rotate = function( n ) {
      
    		 return this.map( (item, index)=> this[ (this.length + index + n)%this.length ] )
    	}

    0 讨论(0)
  • You can use push(), pop(), shift() and unshift() methods:

    function arrayRotate(arr, reverse) {
      if (reverse) arr.unshift(arr.pop());
      else arr.push(arr.shift());
      return arr;
    }
    

    usage:

    arrayRotate(['h','e','l','l','o']);       // ['e','l','l','o','h'];
    arrayRotate(['h','e','l','l','o'], true); // ['o','h','e','l','l'];
    

    If you need count argument see my other answer: https://stackoverflow.com/a/33451102

    0 讨论(0)
  • 2020-11-22 11:05

    So many of these answers seem over-complicated and difficult to read. I don't think I saw anyone using splice with concat...

    function rotateCalendar(){
        var cal=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],
        cal=cal.concat(cal.splice(0,new Date().getMonth()));
        console.log(cal);  // return cal;
    }
    

    console.log outputs (*generated in May):

    ["May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr"]
    

    As for compactness, I can offer a couple of generic one-liner functions (not counting the console.log | return portion). Just feed it the array and the target value in the arguments.

    I combine these functions into one for a four-player card game program where the array is ['N','E','S','W']. I left them separate in case anyone wants to copy/paste for their needs. For my purposes, I use the functions when seeking whose turn is next to play/act during different phases of the game (Pinochle). I haven't bothered testing for speed, so if someone else wants to, feel free to let me know the results.

    *notice, the only difference between functions is the "+1".

    function rotateToFirst(arr,val){  // val is Trump Declarer's seat, first to play
        arr=arr.concat(arr.splice(0,arr.indexOf(val)));
        console.log(arr); // return arr;
    }
    function rotateToLast(arr,val){  // val is Dealer's seat, last to bid
        arr=arr.concat(arr.splice(0,arr.indexOf(val)+1));
        console.log(arr); // return arr;
    }
    

    combination function...

    function rotateArray(arr,val,pos){
        // set pos to 0 if moving val to first position, or 1 for last position
        arr=arr.concat(arr.splice(0,arr.indexOf(val)+pos));
        return arr;
    }
    var adjustedArray=rotateArray(['N','E','S','W'],'S',1);
    

    adjustedArray=

    W,N,E,S
    
    0 讨论(0)
  • 2020-11-22 11:05

    How about incrementing a counter and then getting the remainder of a division by the array length to get where you are supposed to be.

    var i = 0;
    while (true);
    {
        var position = i % months.length;
        alert(months[position]);
        ++i;
    }
    

    Language syntax aside this should work fine.

    0 讨论(0)
  • 2020-11-22 11:05

    ** Using Latest version of JS we can build it every easily **

     Array.prototype.rotateLeft = function (n) {
       this.unshift(...this.splice(-(n), n));
        return this
      }
    

    here moves: number of rotations ,a Array that you can pass random number

    let a = [1, 2, 3, 4, 5, 6, 7];
    let moves = 4;
    let output = a.rotateLeft(moves);
    console.log("Result:", output)
    
    0 讨论(0)
提交回复
热议问题