Rotate the elements in an array in JavaScript
Type-safe, generic version which mutates the array:
Array.prototype.rotate = (function() {
// save references to array functions to make lookup faster
var push = Array.prototype.push,
splice = Array.prototype.splice;
return function(count) {
var len = this.length >>> 0, // convert to uint
count = count >> 0; // convert to int
// convert count to value in range [0, len)
count = ((count % len) + len) % len;
// use splice.call() instead of this.splice() to make function generic
push.apply(this, splice.call(this, 0, count));
return this;
};
})();
In the comments, Jean raised the issue that the code doesn't support overloading of push()
and splice()
. I don't think this is really useful (see comments), but a quick solution (somewhat of a hack, though) would be to replace the line
push.apply(this, splice.call(this, 0, count));
with this one:
(this.push || push).apply(this, (this.splice || splice).call(this, 0, count));
Using unshift()
instead of push()
is nearly twice as fast in Opera 10, whereas the differences in FF were negligible; the code:
Array.prototype.rotate = (function() {
var unshift = Array.prototype.unshift,
splice = Array.prototype.splice;
return function(count) {
var len = this.length >>> 0,
count = count >> 0;
unshift.apply(this, splice.call(this, count % len, len));
return this;
};
})();
Rotate the elements of an array
function rotate(array){
let firstElement = array.shift();
array.push(firstElement);
return array;
}
Rotate the array by d elements in JavaScript
You can use slice
twice and concatenate the subarrays:
const arr = [1,2,3,4,5,6,7], d = 3;
const newArr = [...arr.slice(d % arr.length), ...arr.slice(0, d % arr.length)];
console.log(newArr);
Array Left Rotation in Javascript will log to console but not return
I have also been trying to solve this problem. There are some issues in your current solution. See you have to create an array and store the array passed from parameters in it.
What you have done is just creating a new array and adding the sequence of numbers of elements that should be in it e.g a.length=5
you are doing arr.push 1 2 3 4 5
. But the question wants an array of user's choice. Second thing is that you should return an array not the string.
So this is the solution of this problem:
function rotLeft(a, d) {
var arr = [];
for (var i = 0; i < a.length; i++) {
arr.push(a[i]);
};
for (var j = 1; j <= d; j++) {
arr.shift(arr.push(arr[0]));
}
return arr;
}
rotate an array in JavaScript
function shuffle(o){
for(i = 0; i < o.length; i++)
{
var Index = o.indexOf(i);
index=index+2;
if(index>3)
{
index=index-2;
var item=o.splice(index,1)
o.push(item);
}
else
{var item=o.splice(index,1)
o.push(item)
}
}
};
Rotate positions of elements in array every second in JS
Array.shift()
will remove the first element. Array.push()
will add the element at the end
setInterval
will execute the function every 1000
ms
let array = [1,2,3];
setInterval(()=>{
array.push(array.shift());
console.log(JSON.stringify(array));
}, 1000);
Related Topics
Typeerror: $(...).Modal Is Not a Function with Bootstrap Modal
Clientheight/Clientwidth Returning Different Values on Different Browsers
How to Hover in Reactjs? - Onmouseleave Not Registered During Fast Hover Over
Use :Hover to Modify the CSS of Another Class
CSS Transitions Do Not Work When Assigned Trough JavaScript
Synchronized Scrolling Using Jquery
Mydiv.Style.Display Returns Blank When Set in Master Stylesheet
How to Change Fontsize by JavaScript
Catch Browser's "Zoom" Event in JavaScript
Convert a String to a Template String
Firing a Keyboard Event in Safari, Using JavaScript
Sign PDF with Plain JavaScript
Correct JavaScript Inheritance