-2

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
    );
}
Erythros
  • 123
  • 9

3 Answers3

1

You don't have to update the available cars array every time you want to move an element out of it -- you can update it at the end of the operation. I assume you will place the cars the user has picked into an Object or Map so you can quickly see if the key exists. You could either:

  1. Iterate through your available cars, checking if the car should be moved to a reserved cars array. If it should, push it into the reserved cars array. If not, push it back on to the original array. After the loop ends, splice the available cars array by its original length.
  2. Create another empty array like "newAvailableCars" and instead of pushing cars back onto the original array, push them into this one. After the loop ends, set the original array to be this new one.

Thus in option 1, you would only have to splice once at the end. In option 2, you re-assign your variable to use the new array. The following code shows an example of 5 cars with 2 user picks (a 100x difference in scale, but you should be be able to adapt it just fine). It highlights the first method with the second method commented out.

let availableCars = ['Car 1', 'Car 2', 'Car 3', 'Car 4', 'Car 5'];
let reservedCars = [];
let userPicks = {
  'Car 2': 1,
  'Car 4': 1
};

let length = availableCars.length;
// let newAvailableCars = []; // or use this

for (let i = 0; i < length; i++) {
  if (userPicks[availableCars[i]]) {
    reservedCars.push(availableCars[i]);
  } else {
    availableCars.push(availableCars[i]);
    // newAvailableCars.push(availableCars[i]); // with this
  }
}

availableCars.splice(0, length);
// availableCars = newAvailableCars; // and this

console.log(reservedCars);  // --> [ 'Car 2', 'Car 4' ]
console.log(availableCars); // --> [ 'Car 1', 'Car 3', 'Car 5' ]
skyline3000
  • 7,639
  • 2
  • 24
  • 33
0

Consider creating another array called isCarAvailable with booleans with length equal to availableCars. You can then avoid doing a lot of operations on the availableCars array, such as deleting or adding elements, and instead update the index of car number k in isCarAvailable to 1 for true (available) and 0 otherwise.

Create the array reservedCars[i][j] which contains a user ID i (indexing from 0 or 1 and upwards) and j as the number of cars reserved. Upon initialization: Set the maximum value of i to the number of available cars (length(availableCars)) and j as the maximum number of cars that one user ID can reserve.

0

If you absolutely need to move the items, I'd suggest using lists rather than arrays, as they are better suited for inserting/removing items: Linked list vs Array in Javascript

Otherwise, I'd suggest going with Bent's solution.