让n是数字。然后使用String.slice如下方法:
var output = [], n, padded;
for (n=0; n<=9999; n++) {
padded = ('000'+n).slice(-4); // Prefix three zeros, and get the last 4 chars
output.push(padded);
}
console.log(output); // ["0000", "0001", ..., "9999"]
Run Code Online (Sandbox Code Playgroud)