Is there a way to generate sequence of characters or numbers in javascript?
For example, I want to create array that contains eight 1s. I can do it with for loop, bu
Using Jquery:
$.map($(Array(8)),function(val, i) { return i; })
This returns:
[0, 1, 2, 3, 4, 5, 6, 7]
$.map($(Array(8)),function() { return 1; })
This returns:
[1, 1, 1, 1, 1, 1, 1, 1]
If like me you use linspace a lot, you can modify your version of linspace easily like so:
function linSeq(x0, xN) {
return linspace(x0, xN, Math.abs(xN-x0)+1);
}
function linspace(x0, xN, n){
dx = (xN - x0)/(n-1);
var x = [];
for(var i =0; i < n; ++i){
x.push(x0 + i*dx);
}
return x;
}
You can then use linSeq in any direction, e.g. linSeq(2,4) generates 2,3,4 while linSeq(4,2) generates 4,3,2.
Typescript method based on Ariya Hidayat code:
/**
* Generates sequence of numbers from zero.
* @ param {number} count Count of numbers in result array.
* @ return {Array<number>} Sequence of numbers from zero to (count - 1).
*/
public static sequence(count: number): Array<number>
{
return Array.apply(0, Array(count)).map((x, i) =>
{
return i;
});
}
The fastest way to define an array of 8 1s is to define it-
var A= [1, 1, 1, 1, 1, 1, 1, 1];
// You'd have to need a lot of 1s to make a dedicated function worthwhile.
// Maybe in the Matrix, when you want a lot of Smiths:
Array.repeat= function(val, len){
for(var i= len, a= []; i--; ) a[i]= val;
return a;
}
var A= Array.repeat('Smith',100)
/* returned value: (String)
Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith
*/
Without a for loop, here is a solution:
Array.apply(0, Array(8)).map(function() { return 1; })
The explanation follows.
Array(8)
produces a sparse array with 8 elements, all undefined
. The apply
trick will turn it into a dense array. Finally, with map
, we replace that undefined
the (same) value of 1
.
var GetAutoNumber = exports.GetAutoNumber = (L) => {
let LeftPad = (number, targetLength) => {
let output = number + '';
while (output.length < targetLength) {
output = '0' + output;
}
return output;
}
let MaxNumberOfGivenLength = "";
for (let t = 0;t < L;t++) {
MaxNumberOfGivenLength = MaxNumberOfGivenLength + "9"
}
let StartTime = +new Date();
let Result = [];
let ReturnNumber;
for (let i = 1;i <= MaxNumberOfGivenLength;i++) {
Result.push(LeftPad(i, L))
}
for (let k = 0;k != 26;k++) {
for (let j = 0;j <= 999;j++) {
Result.push(String.fromCharCode(k + 65) + LeftPad(j, (L - 1)));
}
}
console.log(Result.length)
return Result;
}
GetAutoNumber(3)
It will generate result like 001-999, A01-A99... Z01-Z99