Linked Questions
33 questions linked to/from How to initialize an array's length in JavaScript?
-3
votes
2
answers
10k
views
convert number to array in javascript | typescript for angular7 [duplicate]
i have a number 10 how to convert to this to number array in java script.
answer should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
OR How to convert number(10) to number of items in array in javascript
7
votes
1
answer
372
views
What benefit do you get in javascript declaring an array with a specific length? [duplicate]
In most javascript apps I usually declare an array like so
var x = [];
but I've seen a ton of example code on MDN that take this approach instead
var x = new Array(10);
With V8/other modern JS ...
-1
votes
1
answer
271
views
Creating Array in Javascript [duplicate]
How to create an array with length 3 and the value for the array element is 0.
if i give the length of the array as 3, then the array should be like below
a[0]=0;
a[1]=0;
a[2]=0;
I have tried ...
0
votes
2
answers
184
views
How to make array length equal to music duration [duplicate]
Im using Angular 8 and Typescript
Problem: i want an array to have the same length as the music source of an audio tag, to enable comments that are synchronized with the intended location in the track....
2478
votes
81
answers
2.8m
views
How to create an array containing 1...N
I'm looking for any alternatives to the below for creating a JavaScript array containing 1 through to N where N is only known at runtime.
var foo = [];
for (var i = 1; i <= N; i++) {
foo.push(i)...
890
votes
24
answers
434k
views
Repeat a string in JavaScript a number of times
In Perl I can repeat a character multiple times using the syntax:
$a = "a" x 10; // results in "aaaaaaaaaa"
Is there a simple way to accomplish this in Javascript? I can obviously use a function, but ...
165
votes
9
answers
280k
views
javascript create empty array of a given size
in javascript how would I create an empty array of a given size
Psuedo code:
X = 3;
createarray(myarray, X, "");
output:
myarray = ["","",""]
224
votes
6
answers
65k
views
Are trailing commas in arrays and objects part of the spec?
Are trailing commas standard in JavaScript, or do most browsers like Chrome and Firefox just tolerate them?
I thought they were standard, but IE8 puked after encountering one—of course IE not ...
131
votes
4
answers
17k
views
Creating range in JavaScript - strange syntax
I've run into the following code in the es-discuss mailing list:
Array.apply(null, { length: 5 }).map(Number.call, Number);
This produces
[0, 1, 2, 3, 4]
Why is this the result of the code? What's ...
27
votes
3
answers
25k
views
Javascript TypedArray performance
Why are TypedArrays not as fast as regular arrays? I want to store some precalculated integer values, and I need the access to the array to be as fast as it can be.
http://jsperf.com/array-access-...
16
votes
10
answers
21k
views
Functional Programming - Simple For Loop For Incrementing Counter
We dont use for loop in functional programming, instead, we use higher order functions like map, filter, reduce etc. These are fine for iterating through an array.
However, I wonder how do I do a ...
15
votes
3
answers
63k
views
functional loop given a number instead of an array [duplicate]
Say I have a number 18, instead of an array, in hand.
What is the best way to create a functional loop in JS given a number X instead of array of X elements?
I can do this:
[1,2,3].forEach(function(...
8
votes
3
answers
7k
views
Pre-allocate memory to array of objects
I have an array declared as
var arr = new Array();
Then i have an array of objects which are returned by Server. And each object in this array has three fields(always). I have to loop through this ...
4
votes
5
answers
5k
views
Best way to populate array with strings
Say I have an array :
var newArray = [];
I can add strings to it like so :
var thisString = 'watch';
newArray.push(thisString);
What I want to do is have an array of this string. So, for example, I ...
2
votes
3
answers
10k
views
How to add undefined element to array?
It's simple, i want to add an undefined element to an array, lets say i have an array Punkt and i have punkt[0] = x: 15, y:"16s" . As i know for ex. the element will have 4 punkt in total i want to ...