javascript – 旋转数组并将所有组合存储在对象变量中

前端之家收集整理的这篇文章主要介绍了javascript – 旋转数组并将所有组合存储在对象变量中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有月份数组,应该在其长度时间内从左向右旋转.将所有旋转数组存储在对象变量中.能否请您提出更有效的方法.

var Month = ["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"];


 Output looks like:

 monthRotate = {
                      rotate1: ["Feb","Dec","Jan"],rotate2: ["Mar","Jan","Feb"],.
                       .
                       . 
                       .
                      rotate11: ["Dec","Nov"]; 
             }

我试过这个方法.

var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var rotate = {};
for (var i=1;i
最佳答案
您可以使用shift方法删除给定数组的第一个元素,然后将其推送到数组的末尾.

var months = ["Jan","Dec"];
let final = [...Array(months.length-1)].reduce(function(arr){
   months.push(months.shift());
   arr.push([...months]);
   return arr;
},[]);
console.log(final);

猜你在找的JavaScript相关文章