I've written a function that moves selected items from a list into a new position. I've found that in order to do this, I need to retain the original 'index structure' of the array. The way I've done this is, instead of splicing the array right away, I set the elements to null, then later traverse the list backwards and splice the null elements. I don't know if this is the best way of doing this, but I thought I would post it here to see if anyone else has an idea of how to better accomplish this.
https://jsfiddle.net/g407pjbk/1/
function drag_and_drop(list, before_index){
const selected = [];
for(let index in list){
const item = list[index];
if(item.selected){
selected.push(item);
// Set elements to null in order to retain indecies
list[index] = null;
}
}
// Insert selected items
list.splice(before_index, 0, ...selected);
// Backwards traverse and splice remaining null elements
for(let i=list.length-1;i>=0;i--){
if(list[i] == null)
list.splice(i, 1);
}
return list;
}