Skip to content

Instantly share code, notes, and snippets.

@bdogan
Created February 22, 2018 11:18
Show Gist options
  • Save bdogan/14d3b2aad6a075d14737e04813d772f0 to your computer and use it in GitHub Desktop.
Save bdogan/14d3b2aad6a075d14737e04813d772f0 to your computer and use it in GitHub Desktop.
Move rows in array
/**
* Move row by source / target
*
* Column Count = 4
* [
* 1 , 2 , 3 , 4,
* 5 , 6 , 7 , 8, * (SourceRow = 2, elCount = 1)
* 9 , 10, 11, 12,
* 13, 14, 15, 16, * (TargetRow = 4)
* 17, 18, 19, 20,
* ]
*/
function moveRow(sourceArray, sourceRow, targetRow, columnCount, elCount) {
sourceArray = sourceArray.slice(0);
var sourceStartIndex = ((sourceRow - 1) * columnCount);
var source = sourceArray.slice(sourceStartIndex, (sourceStartIndex + (columnCount * elCount)));
sourceArray.splice(sourceStartIndex, (columnCount * elCount));
targetRow = targetRow - 1;
if (targetRow > sourceRow) targetRow = targetRow - elCount;
sourceArray.splice.apply(sourceArray, [(targetRow * columnCount), 0].concat(source));
return sourceArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment