I want to store or save data in js object as follow:
var car= {};
car[number] = user_input;
giving...
car = {1:"Fiat"};
But I only want to have up to max three values.
car = {1:"Fiat", 2:"Honda"};// two values
car = {1:"Fiat", 2:"Honda", 3:"Ford"}; //three values
So, if there is another input after three values are already added, then I want it so that it will replace the first value like below:
car = {1:"Nissan", 2:"Honda", 3:"Ford"};
Then the second value will be replaced next and so on:
car = {1:"Nissan", 2:"Tesla", 3:"Ford"};
What would be an elegant way of designing this?
Any suggestions will be much appreciated.
EDIT:
To clarify the question, the key is id number such as "234" or "5432" or "2342".
For example:
car = {1233:"Nissan", 555:"Tesla", 3345:"Ford"};
Lots of value pairsor only 3 (in the question you have mentionedmax 3 values)? Is your key always going to be integer?