Array Generator: Initialize arrays using several methods

Recommend this page to a friend!
  Info   Documentation   View files Files   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 138 All time: 360 This week: 4Up
Version License JavaScript version Categories
array-generator 1.0Freely Distributable5Data types
Description 

Author

This is an extension of the Array class that can initialize arrays using several methods.

It provides several new functions for the Array that can setup the elements of the arrays with specific values. Currently it can:

- Populate the array with a given number of keys
- Set the array elements with a range of values between two numbers
- Set the array elements with an incremented or decremented range of numbers starting from the element 0 to a given number
- Set the array elements with a given range between two numbers but having a given gap between each element
- Set the array elements with a range of letters of a given length starting with A
- Repeat the value of the element 0 a given number of times
- Set the array elements with values of a given formula callback

Innovation Award
JavaScript Programming Innovation award nominee
April 2015
Number 4
Some applications use large arrays populated with data that is convenient for the applications' purposes.

This package provides several ways to pre-populate arrays in several ways that are useful to some of those applications.

Manuel Lemos
Picture of Stephen Chapman
  Performance   Level  
Name: Stephen Chapman <contact>
Classes: 11 packages by
Country: Australia Australia
Age: ???
All time rank: 171 in Australia Australia
Week rank: 6 Up1 in Australia Australia Equal
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Documentation

Generating Arrays

These have all been set up as methods added to the built in Array object for consistency. In most cases the []. on the front of the call acts simply as a reminder that the method returns an array. The exceptions actually use that array to pass in an initial value for the method to use as the first entry in the array.

Each of these seven methods creates a dense array in JavaScript containing values based on the parameters that are passed to it. While the examples below are creating small arrays that you could just as easily type in manually, these methods allow the creation of much larger arrays simply by supplying the appropriate parameters.

populate

myArray = [].populate(10);

Creates an array with the entries having values corresponding to their position in the array. For example the above call creates the array [0,1,2,3,4,5,6,7,8,9]

You can populate arrays as big as you require (up to the maximum array size of 4294967294) simply by specifying the number of elements you want in the array and each position will be given a value that is the same as its index.

range

myArray = [].range(-5,10);

creates an array with entries in the specified range. For example the above call creates an array containing [-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]

Note that it doesn't matter which way around you specify the two values, this method always returns an array of integers in ascending order.

to

myArray = [10].to(-5);

similar to the range method except that this one reads the starting value from the array the method is attached to and so can produce arrays with the integers in descending order. For example the above call creates the array [10,9,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5]

intervals

myArray = [].intervals(1,10,0.5);

unlike the above methods which populate the array with integers, this method can apply any fixed interval between the values. The first parameter supplies the starting point, the second is the length of the array to be produced and the third is the interval between the values (which defaults to 1 if not supplied). For example the above call creates the array [1,1.5,2,2.5,3,3.5,4,4.5,5,5.5]

To reverse the direction of the values in the array simply specify a negative gap.

alpha

myArray = [].alpha[40];

not numbers this time but uppercase letters of the alphabet that simply repeat the alphabet as many times as required to fill the array. For example the above call creates the array ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N']

repeat

myArray = ['#'].repeat(10);

puts the same value in all the entries in the array but allows you to specify what value that is. For example the above call creates the array ['#', '#','#','#','#','#','#','#','#','#']

plot

f = function(x) {return x * 2;}; myArray = [].plot(f,0,5,0.5);

The most complex of the array methods generates an array of objects where each object consists of X and Y coordinates. First it calls the intervals method using the second through fourth parameters supplied to generate the X values. It then uses the function supplied in the first parameter to generate a corresponding Y value for each of the X values using that formula. For example the above call creates the array [{x:0,y:0},{x:0.5,y:1},{x:1,y:2},{x:1.5,y:3},{x:2,y:4}]

hasIndex

for (k in myArray) { if (myArray.hasIndex(k)) { // code for processing array entries goes here - only array entries will be processed as the if statement skips over other properties and methods } }

this one doesn't generate an array, instead it provides a safe way to use for-in loops with arrays where additional methods or properties have been added to either the array itself or to the array prototype (as I have done with all of the array methods provided here).


  Files folder image Files (2)  
File Role Description
Plain text file arraygen.js Class Methods for generating arrays
Accessible without login Plain text file arraygen.txt Doc. Documentation for the array methods

 Version Control Unique User Downloads Download Rankings  
 0%
Total:138
This week:0
All time:360
This week:4Up