There simple code:
var arr = [];
for(var i = 0; i < 10; i++){
arr.push("some string with iterating value "+i);
}
I wonder if there is a one-line solution in which I enter the string and the maximum value and get an array of generated lines.
Try this, if the input is 5, you can have it as N and get the input from user
DEMO
var result = Array.from(new Array(5),(val,index)=> "some string " + index );
console.log(result);
const newArray = [...Array(10)].map((_, i) => `some string with iterating value ${i}`)
console.log(newArray)
You can use a spread operator and create a new array of the length you require, loop (map) over it and return the string. This will create a new array of the length (10) with the string you want in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With