What is the most efficient way to remove multiple items by index and save them in arrays actives
and availables
? I need to move selected elements from a ListBox, represented as selectedValues = [1, 4, 2]
.
Here's my proposed solution:
var actives = [ "a", "d", "k", "e"]
var availables = [ "m", "o", "v" ]
var selectedValues = [3, 1]
var elementsToMove = []
selectedValues.forEach(i => {
elementsToMove.push(actives[i])
})
actives = actives.filter(item => !elementsToMove.includes(item))
availables = availables.concat(elementsToMove);
console.log(actives);
console.log(availables);
Expected result:
actives = [ "a", "k" ]
availables = [ "m", "o", "v", "e", "d"]
Note: The size of the arrays can vary greatly.