2

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"};    
6
  • 2
    are you sure about an object, if an array would fit better? Commented Feb 7, 2017 at 7:37
  • yeah, it will have to be an object as there will lots of value pairs. Commented Feb 7, 2017 at 7:38
  • @steveKim thats no problem for an array either Commented Feb 7, 2017 at 7:40
  • Mhmm, I would definitely look into it. =) Commented Feb 7, 2017 at 7:41
  • Lots of value pairs or only 3 (in the question you have mentioned max 3 values)? Is your key always going to be integer? Commented Feb 7, 2017 at 7:42

3 Answers 3

2

You can use remainder operator and number % 3 in the property, if your number will change it's value by 1.

number % 3 - this will keep your index from 0 up to 2. So if you enter the 4th element, it's index will be 4 % 3 === 1, if you starts from 1.

var car= {};
car[number % 3] = user_input; 

Example

var car= [];
var number = 0;
var input;

while(number < 10){
  input = prompt();
  car[number % 3] = input;
  console.log(car);
  number++;
}

With ids you can store them and use the number to get the appropriate one.

var ids = [234, 4641, 1352];

var car= {};
var number = 0;
var input;
var ids = [234, 4641, 1352];

while(number < 10){
  input = prompt();
  car[ids[number % 3]] = input;
  console.log(car);
  number++;
}

Sign up to request clarification or add additional context in comments.

6 Comments

@steveKim see. Edited
Thank you for the great information. Just a quick question. Let say the key is not 0-2 number, but rather an id (such as "234", "4641" or "1352"). So the key is not from 0 to 2, but rather a random number. In this case, how would I apply the same concept?
Here we choose our indexes from the number. With the id's it must be changed. For every time your id's are different or they stay the same
@steveKim, if the given keys are necessary, please add this information to the question above.
Same comment: Why do you need the index count? OP says that the user will input the index number?
|
2

You could use the remainder operator % and move the value into the range. Then add one for the one based interval.

var car = {},
    insertCar = function (index) {
        return function (carName) {
            car[index + 1] = carName;
            index++;
            index %= 3;
        }
    }(0);


insertCar('Fiat');
console.log(car);
insertCar('Honda');
console.log(car);
insertCar('Ford');
console.log(car);
insertCar('Nissan');
console.log(car);
insertCar('Tesla');
console.log(car);
.as-console-wrapper { max-height: 100% !important; top: 0; }

With give keys [234, 4641, 1352] and round robin

var car = {},
    insertCar = function (index) {
        var keys = [234, 4641, 1352];
        return function (carName) {
            car[keys[index]] = carName;
            index++;
            index %= keys.length;
        }
    }(0);


insertCar('Fiat');
console.log(car);
insertCar('Honda');
console.log(car);
insertCar('Ford');
console.log(car);
insertCar('Nissan');
console.log(car);
insertCar('Tesla');
console.log(car);
.as-console-wrapper { max-height: 100% !important; top: 0; }

With an array, as suggested

var car = [],
    insertCar = function (index) {
        return function (carName) {
            car[index] = carName;
            index++;
            index %= 3;
        }
    }(0);


insertCar('Fiat');
console.log(car);
insertCar('Honda');
console.log(car);
insertCar('Ford');
console.log(car);
insertCar('Nissan');
console.log(car);
insertCar('Tesla');
console.log(car);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

Why do you need the index count? OP says that the user will input the index number?
@PritamBanerjee, the op does not know, what to use. a comment shows, that possible an array of keys is used ... but who knows.
0

You can use something like this:

var carCount = "Take this as UserInput";
var newValue = "Take the value as Input as well"

if (carCount < 3){
  (car[carCount] = newValue);
}

2 Comments

I upvoted. It's simple but I can see how it can be used. I am not sure who downvoted.
@steveKim If you liked my answer can you please accept the answer? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.