我有4个整数变量:A,B,C& D.
我想要一个名为cycle()的函数,其中A的值变为B,B的值变为C,C的值变为D,D的值变为A.
我该怎么做?
答案 0 :(得分:9)
ES6允许您使用destructuring assignment执行此操作:
[a, b, c, d] = [b, c, d, a];
([d, a, b, c]
如果“D进入A”意味着A应该得到D的值)
答案 1 :(得分:2)
您可以使用shift
和function cycle(arr){
arr.push(arr.shift());
// if you want d goes to a then
//arr.unshift(arr.pop());
return arr;
}
var a = 1, b= 2, c = 3, d = 4;
[a,b,c,d] = cycle([a,b,c,d]);
console.log(a,b,c,d);
来执行此操作。它将支持N个元素旋转。
Set