beannshie
beannshie

Reputation: 89

How to set all item values of an array to 0?

I'm trying to find out what's the simplest way to set all the items' values of an array to (in this case) 0 (not null, just "0").
This is my array :

var usernameAlphaLog = [localStorage.getItem("user1-log"), localStorage.getItem("user2-log"), localStorage.getItem("user3-log")...]

I'm not putting the local storage items in a variable becouse I find that it works better this way (at least for me).

Basically I want to set all of these items' values to 0.
Because I'm pretty new to javascript if it's possible, do it in the simplest way, if not the please, if you have an answer, explain it to me.

Thank you.

Upvotes: 0

Views: 523

Answers (3)

Clive Ciappara
Clive Ciappara

Reputation: 558

I think you need something like this:

int count =13; //the number of user-logs that you have
for(int i = 1; i <= count; i++) {
    localStorage.setItem("user" + i + "-log", 0);
    //usernameAlphaLog.push(localStorage.getItem("user" + i + "-log");
}

If you want to store the values in an array, you can do like above or add it with the above:

var usernameAlphaLog = [ ];
for(int i = 1; i <= count; i++) {
    usernameAlphaLog.push(localStorage.getItem("user" + i + "-log");
 }

Upvotes: 0

rjustin
rjustin

Reputation: 1439

usernameAlphaLog = usernameAlphaLog.map(x => x = 0);

Upvotes: 1

Danny Mcwaves
Danny Mcwaves

Reputation: 179

usernameAlphaLog.fill(0) should replace all values with 0.

Upvotes: 1

Related Questions