vendredi 21 décembre 2018

Looking for a better approach to store a list of dynamically created objects in javascript

I have an object let's name it Passengers which contains a list of names according to their default rank. Eg. -

Passengers {
       P1 : {Ticket Price:<x>, Distance:<x> ---},
       P2 : {Ticket Price:<x>, Distance:<x>---},
       |
       |
       Pn : {Ticket Price:<x>, Distance:<x>---}
      }

Now Based on some input, I am changing the ranking of the list of passengers & storing them in a new object. The input will be decided dynamically by the user. Like user can say I want to rank on the basis of ticket price, distance etc.
The catch here is user can go & come back to any ranking state at any time. Eg. - User first asks to rank by distance, then asks to rank by ticket price, then again asks to rank by distance. In this kind of scenario where user again asked to rank the list by distance , I don't want to recalculate the ranking logic as it's a costly affair. The ranking criteria can be many & are also decided dynamically. The most straightforward approach for reusability would be to store these objects in a Map where key is ranking criteria & value is its respective object.
Is there any design pattern or a better way in Javascript through which it can be achieved in a cleaner way. I went through Object state & Memento design pattern but these are not making much of sense for this requirement.
Any suggestions.

Aucun commentaire:

Enregistrer un commentaire