Building the Right Environment to Support AI, Machine Learning and Deep Learning
Creating an Array
An array is created using the
new keyword. This creates a new instance of the object. Basically this means that we take the array object, make a copy of it and give that copy a name. It's a new "instance" or "occurrence" of the array object. The format for creating a new instance of an array is:
var variableName = new Array();
The format should be pretty familiar to you by now. First, we declared a variable
var keyword. Then we used the "
= " operator
to equate the variable to the new instance of the array. Finally, we created
a new instance of the array object using the
new keyword. Since
the array is an object, we placed a semi-colon ("
; ") after it.
The variable creates a reference to the new instance of the array in order
to access its data. Remember, a variable is only a data container, so in this
case, the variable "contains" a reference to the array to which it is referenced.
Let's return to our column of data example. Many times a group of data in a
spreadsheet is given a name to make it easier to refer to it in calculations
and macros. Instead of writing "
=SUM(B7:C16+F7:G15)," which is
difficult to remember what it represents, we could give the cells names and
=SUM(sales + bonus)," That's much easier to understand.
When we use a variable to reference an array it's basically the same thing.
One method creates an empty instance of an array and then populates it. In this type of array, each element is assigned an index number by the person writing the code, i.e. you.
var fruits = new Array(); fruits="apples"; fruits="oranges"; fruits="peaches";
An array can also be populated by listing each piece of data in a comma-delimited format. This method is called a dense array:
var myCar = new Array("Chevrolet","Honda","Buick","Ford");
In this example each element is given as an argument to the
object. Remember, objects are created with the format:
with the parentheses following it. The parentheses can contain an argument to
be used by the object. In this case, it's storing data for use by the
object. Using this format, the index numbers are assigned automatically by the
The difference between the first two different methods is that the first method is good for a large amount of data and the second method is better for a small amount of data. You can see where, using the second method, a large amount of data could become very confusing, i.e.:
var myCar = new Array("Chevrolet","Honda","Buick","Ford","Rolls Royce","Volkswagen","Dodge","Pontiac", "Chrysler","Alfa Romeo","Cadillac","Datsun","Fiat","Hyundai","Jeep","Mazda");
as opposed to:
var myCar = new Array() new myCar="Chevrolet"; new myCar="Honda"; new myCar="Buick"; new myCar="Ford"; new myCar="Rolls Royce"; new myCar="Volkswagen"; new myCar="Dodge"; new myCar="Pontiac"; new myCar="Chrysler"; new myCar="Alfa Romeo"; new myCar="Cadillac"; new myCar="Datsun"; new myCar="Fiat"; new myCar="Hyundai"; new myCar="Jeep"; new myCar="Mazda";
As you can see, the second one is much easier to read. It takes more time, but is worth it in the long run.
The third method for creating an array is by using literal notation. The format is:
var myCar = ["Hyundai","Jeep","Mazda"]
It's similar to the one above except it doesn't use the
object constructor and it uses brackets instead of parentheses. The
mean to create an array using the given data for the elements. This method can
be interpreted by browsers that are version 4.0 and later.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...