简体   繁体   中英

JavaScript move along all items in array by X number

I'm wanting to create a function which accepts 2 arguments, first argument is an array, second argument is a number of index positions to move all the array items.

So for example if I passed exampleFunc([1,2,3,4,5], 2) it should move all items 2 places to the right, so returns [4,5,1,2,3]. I've done the following, however is there a more eloquent / efficient way of doing this? Also if I wanted to reverse the direction and condense into 1 function and not two as done below, any suggestions how to do this other than putting conditionals around the different part of each function? Tried using .splice() method but didn't really got anywhere. Any help would really be appreciated!

 const moveArrayPositionRight = (array, movePositions) => { let newArray = new Array(array.length); for (i = 0; i < array.length; i++) { let newIndex = i - movePositions; if (newIndex < 0) { newIndex += array.length; } newArray[i] = array[newIndex]; } return newArray; }; console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 2)); // output: [8, 10, 2, 4, 6]

 const moveArrayPositionLeft = (array, movePositions) => { let newArray = new Array(array.length); for (i = 0; i < array.length; i++) { let newIndex = i - movePositions; if (newIndex < 0) { newIndex += array.length - 1; } newArray[i] = array[newIndex]; } return newArray; }; console.log(moveArrayPositionLeft([3, 6, 9, 12, 15], 2)); // output: [9,12,15,3,6]

You have the index of the position where you want to slice the array up and rearrange it, so you can use .slice to do exactly that - extract the sub-arrays that need to be rearranged, and put into a new array:

 const moveArrayPositionRight = (array, movePositions) => [ ...array.slice(array.length - movePositions), ...array.slice(0, array.length - movePositions) ]; console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 2)); // output: [8, 10, 2, 4, 6] console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 3)); // expected [6, 8, 10, 2, 4]

.slice can also take negative indicies to slice an amount from the end instead of from the beginning:

 const moveArrayPositionRight = (array, movePositions) => [ ...array.slice(-movePositions), ...array.slice(0, -movePositions) ]; console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 2)); // output: [8, 10, 2, 4, 6] console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 3)); // expected [6, 8, 10, 2, 4]

Can also use .concat instead of spread

 const moveArrayPositionRight = (array, movePositions) => array .slice(array.length - movePositions) .concat(array.slice(0, array.length - movePositions)); console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 2)); // output: [8, 10, 2, 4, 6] console.log(moveArrayPositionRight([2, 4, 6, 8, 10], 3)); // expected [6, 8, 10, 2, 4]

Same sort of thing for moveArrayPositionLeft :

 const moveArrayPositionLeft = (array, movePositions) => [ ...array.slice(movePositions), ...array.slice(0, movePositions) ]; console.log(moveArrayPositionLeft([3, 6, 9, 12, 15], 2)); // output: [9,12,15,3,6]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM