beannshie beannshie - 3 years ago 134
Javascript Question

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.

Answer Source

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");
 }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download