JavaScript Generating a filled array in javascript [duplicate]

This question already has answers here:
Answer:1

So. I would expect the following code to return [1,1,1,1...].

(new Array(10)).map(function() { return 1;}) But it returns [, , , , , ...].

Right, because new Array(10) creates an array with no elements with a length of 10, and map only iterates over elements that actually exist. (And yes, this is surprising. :-) )

Moreover, (new Array(10)).length == 10 and (new Array(10))[0] == undefined are true.

Again correct, because (again) new Array(10) doesn't put any elements in the array, so accessing [0] gives you undefined.

JavaScript's standard arrays aren't really arrays at all, and they can have a length that's a positive number without having any entries in them. They're a form of "sparse" array.

Let's take a simpler example:

var a = new Array(10);
a[2] = 1;

That array contains one element, the element at index 2. There is no element at index 0, no element at index 1, and no elements at indexes 3 and above. It just has gaps there. You can tell by asking it:

console.log(0 in a); // "false"
console.log(1 in a); // "false"
console.log(2 in a); // "true"

Standard arrays in JavaScript are just objects with special behavior assigned to length, special behavior assigned to a class of property names (loosely, numeric ones), and that are backed by Array.prototype.

This is all in contrast to the newer "typed" arrays, Int32Array and such, which are true arrays in the traditional sense.

Answer:2

This will work:

Array.apply(null, Array(10)).map(…

Live demo: http://jsfiddle.net/xXG5p/

You have to make the array dense first. new Array(n) creates a sparse array. Sparse arrays do have a length but they have no elements. Hence, a .map() call will not iterate at all.

Using my code above, you will create a dense array that does contain elements (whose values are initially set to null).

Answer:3

Question Background: I'm learning AngularJS. I have created a simple app that takes in 3 form inputs, and on submission of this form a paginated list should be rendered. I have a dependancy on UI-...

Question Background: I'm learning AngularJS. I have created a simple app that takes in 3 form inputs, and on submission of this form a paginated list should be rendered. I have a dependancy on UI-...

I made a table that has checkboxes that show and hide rows to make it easier to compare specific rows. I wanted to add indeterminate checkboxes so that multiple rows can be selected or not selected at ...

I made a table that has checkboxes that show and hide rows to make it easier to compare specific rows. I wanted to add indeterminate checkboxes so that multiple rows can be selected or not selected at ...

I'm trying to crawl a webpage using Nightmare, but want to wait for #someelem to be present, only if it actually exists. Otherwise, I want Nightmare to move on. How can this be done using .wait()? I ...

I'm trying to crawl a webpage using Nightmare, but want to wait for #someelem to be present, only if it actually exists. Otherwise, I want Nightmare to move on. How can this be done using .wait()? I ...

I'm trying to figure out how to trigger a link button to trigger and change all checkboxes to checked when clicked upon. When I click the button, it will bring up the alert message, but not triggering ...

I'm trying to figure out how to trigger a link button to trigger and change all checkboxes to checked when clicked upon. When I click the button, it will bring up the alert message, but not triggering ...

  1. trigger check checkbox jquery
  2. trigger checkbox check
  3. trigger checkbox check event