-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (25 loc) · 990 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export default function move (array, moveIndex, toIndex) {
/* #move - Moves an array item from one position in an array to another.
Note: This is a pure function so a new array will be returned, instead
of altering the array argument.
Arguments:
1. array (String) : Array in which to move an item. (required)
2. moveIndex (Object) : The index of the item to move. (required)
3. toIndex (Object) : The index to move item at moveIndex to. (required)
*/
let itemRemovedArray = [
...array.slice(0, moveIndex),
...array.slice(moveIndex + 1, array.length)
]
return [
...itemRemovedArray.slice(0, toIndex),
array[moveIndex],
...itemRemovedArray.slice(toIndex, itemRemovedArray.length)
]
}
// Examples
// --------
move(['a', 'b','c'], 2, 0)
// -> ['c', 'a', 'b']
move([{name: 'Fred', name: 'Barney', name: 'Wilma', name: 'Betty'}], 2, 1)
// -> [{name: 'Fred', name: 'Wilman', name: 'Barney', name: 'Betty'}]