About 71,400 results
Open links in new tab
  1. What is the way of declaring an array in JavaScript?

    The 2nd example makes an array of size 3 (with all elements undefined). The 3rd and 4th examples are the same, they both make arrays with those elements. Be careful when using new Array(). var x = new Array(10); // array of size 10, all elements undefined var y = new Array(10, 5); // array of size 2: [10, 5] The preferred way is using the ...

  2. How to initialize an array's length in JavaScript?

    Jan 31, 2011 · Defining JavaScript Arrays and Array Entries 1. JavaScript Array Entries. JavaScript pretty much has 2 types of array entries, namely mappable and unmappable. Both entry types are iterable, meaning they both work with the for-of loop. 2. JavaScript Arrays. A JavaScript array can contain both mappable and unmappable array entries.

  3. Initializing an Array with a Single Value - Stack Overflow

    Oct 29, 2010 · let array = new Int32Array(arraySize) // this array is already 0-initialized array.fill(value) // only necessary if value != 0 For arraySize = 1e7 with value = 1 this only takes 4ms on my machine, as opposed to 15ms for the fill method and 70ms for the push method.

  4. How do I create JavaScript array (JSON format) dynamically?

    The preferred way of creating a array in javascript is var employess = []; not var employees=new Array(); – Mattias Jakobsson Commented Feb 12, 2010 at 10:08

  5. Initializing a javascript array - Stack Overflow

    Aug 11, 2011 · You initialized an array when what you're most likely looking for is a hash. counter will still return [] and have a length of 0 unless you change the first line to counter = {}; . The properties will exist, but it's a confusing use of [] to store key-value pairs.

  6. Most efficient way to create a zero filled JavaScript array?

    Not really, Array.from() here is basically creating an array, iterating through it with map(), calling a function on each item to create a new array, then discarding the first array... For a small arrays this may be innocuous, for larger arrays, this is the kind of pattern that result in people calling browsers "memory hogs" :)

  7. How can I create a two dimensional array in JavaScript?

    To create a 2D array in javaScript we can create an Array first and then add Arrays as it's elements. This method will return a 2D array with the given number of rows and columns. function Create2DArray(rows,columns) { var x = new Array(rows); for (var i = 0; i < rows; i++) { x[i] = new Array(columns); } return x; }

  8. javascript - How to create an array containing 1...N - Stack Overflow

    // `n` is the size you want to initialize your array // `null` is what the array will be filled with (can ...

  9. javascript - Declaring array of objects - Stack Overflow

    Apr 1, 2013 · EDIT: If your goal is an array whose undefined items are empty object literals by default, you can write a small utility function: function getDefaultObjectAt(array, index) { return array[index] = array[index] || {}; } Then use it like this: var sample = []; var obj = getDefaultObjectAt(sample, 0); // {} returned and stored at index 0. Or even:

  10. Is there a functional way to init an array in JavaScript ES6?

    Jul 26, 2018 · You can pass a {length: somlength} object or some other array-like object and a function that defines each item. The first argument (calling it _ just to indicate it's not used) to that function would be the item from an array we passed in (but we only passed in a length so it doesn't mean much), the second i is the index, which is used for ...

Refresh