I have an array called availableCars
that has about 500 elements. The user can select some of them (let's say 200, randomly) and "reserve" those cars.
I need, therefore, onclick to move all those selected items from availableCars
array to reservedCars
array.
Iterating all the items and doing a splice
in order to remove them from first array and push the element to the 2nd array seems complicated, but I'm not sure if there's a better and optimal way.
Suggestions ?
P.S. The solution I came up with is the following:
public moveCarsAround(selectedElements): void {
// Add all selected elements to the reservedCars list.
this.reservedCars = this.reservedCars.concat(this.availableCars.filter((car) =>
selectedElements.indexOf(car.id) !== -1
));
// Remove all selected elements from availableCars list.
this.availableCars = this.availableCars.filter((car) =>
selectedElements.indexOf(car.id) === -1
);
}