-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayMethods.js
31 lines (22 loc) · 1.05 KB
/
arrayMethods.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
// testing moving tracks
const paylistTracks = [{name: 't1', id: 't1'}, {name: 't2', id: 't2'}, {name: 't3', id: 't3'}];
function moveTrack(trackToMove, arr, direction) {
let origTrackPosition = arr.findIndex(track => track.id === trackToMove.id);
arr.splice(origTrackPosition, 1); // remove track
let newTrackPosition;
direction === 'up' ?
newTrackPosition = origTrackPosition - 1
: newTrackPosition = origTrackPosition + 1;
if (newTrackPosition < 0 || newTrackPosition > arr.length) {
console.log('Ran outta playlist! Resetting array...');
arr.splice(origTrackPosition, 0, trackToMove); // reinsert track
arr.sort((a, b) => a.id[1] - b.id[1]); // sort track
return;
}
arr.splice(newTrackPosition, 0, trackToMove); // move track
console.log(JSON.stringify(arr));
}
moveTrack({name: 't3', id: 't3'},paylistTracks, 'up');
moveTrack({name: 't3', id: 't3'},paylistTracks, 'up');
moveTrack({name: 't3', id: 't3'},paylistTracks, 'up');
// moveTrack({name: 't3', id: 't3'},tracks, 'down');