1

I am looking to do something very similar to the following PHP code but in javascipt:

for ($weenumber = 1; $weenumber <= 30; $weenumber++)
{ 
    $weenumber = str_pad($weenumber, 2, "0", STR_PAD_LEFT);
    echo $_POST["entry{$weenumber}_cash"];
} 

Basically accessing the loop number padded with a trailing 0 if less than 10 but I dont know the syntax in JS to do this :(

Sorry for noob style question

3
  • 1
    You mean "padding with a leading 0 if less than 10", right? Commented Sep 2, 2011 at 11:54
  • Thats one part of it, the main thing is the syntax to reference a variable inside a variable : entry{$i} in PHP would give : entry01 for instance Commented Sep 2, 2011 at 11:55
  • Google what exactly Jon? I did try "referencing variables in javascript" et al with no joy Commented Sep 2, 2011 at 11:56

7 Answers 7

2

I think that you mean a leading zero rather than a trailing zero...

You can for example use the conditional operator:

(n < 10 ? '0' : '') + n

You could also implement a general purpose function:

function padLeft(str, len, ch) {
  while (str.length < len) str = ch + str;
  return str;
}

To access an object property by name in Javascript you use brackets. Example:

var value = obj['entry' + (n < 10 ? '0' : '') + n + '_cash'];

If n contains 4, this will be the same as obj.entry04_cash.

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

1 Comment

Thanks for all the help from everyone and of course Guffa. Really love this site and the people who help out :) Thanks again everyone!
0

Whether or not there's a specific function to do this, if you know how to use an if clause, and you know how to perform string concatentation (using the + operator if you didn't), then you should be able to easily roll you own version of str_pad by hand (which works for numbers below 100).

Think about the cases involved (there are only two) and what you need to output in either case.

Comments

0

This is the code you should use:

for(var i=0; i<30; i++){
    document.writeln(i<10 ? "0"+i : i);
}

change document.writeln() with any function you want to handle the data

Comments

0
for (weenumber = 1; weenumber <= 30; weenumber++) { 
    weenumber = str_pad(weenumber, 2, "0", STR_PAD_LEFT);
} 

For the str_pad() function, you can use PHPJS library: http://phpjs.org/functions/str_pad:525

This library will also ease transition from php to javascript for you. Check it out.

Comments

0
for(var i=0; i<30; i++)
{
   var index = i;
   if(i<10) index = "0" + index
   var elem = document.getElementById("entry"+index);
}

Comments

0
var someArray = [/*...*/];
for (var i= 1;i<= 30;i++)
    { 
        var weenumber = i+"";
        for(var j=weenumber.length;j<2;j++)
            weenumber = "0" + weenumber;
        var key = "entry" + weenumber + "_cash";
        document.write(someArray[key]);
    }

Comments

0

Here's a function you can use for zeropadding:

function zeroPad(nr,base){
        var  len = (String(base).length - String(nr).length)+1;
        return len > 0? new Array(len).join('0')+nr : nr;
}
//usage
alert(zeroPad(3,10)); //=> 03

or extend the Number prototype

Number.prototype.zeroPad = Number.prototype.zeroPad ||
    function(base){
            var nr = this, len = (String(base).length - String(nr).length)+1;
            return len > 0? new Array(len).join('0')+nr : nr;
    };
//usage
var num = 1;
alert(num.zeroPad(100)); //=> 001

Now for the variable name part: if it's a global variable (not advisable) that variable is a property of the global object, in a browser that's window. You can get a variable by its dynamic name using the equivalent of: window['myvariable'+myJustResolvedNumericValue]. Within an object (instance) you can use the same bracket notation: myObject['myvariable'+myJustResolvedNumericValue].

Using this information, in javascript your function could look like:

for (var weenumber = 1; weenumber <= 30; weenumber++)
{ 
    // execute something using the variable that uses the counter in the
    // variable name as parameter
    executeSomeFunction(window['entry'+weenumber.padLeft(10) + '_cash']);
} 

Comments

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.