DevelopPHP
home gear home donate
Search Results For
in:

Libraries book

HTML Library CSS Library JavaScript Library PHP Library

Videosbook

PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous

Resourcesbook

Website Hosting Developer Q+A
Sprite Programming Mastery
Array Object Javascript

Arrays supply a programmer with a way of compartmentalizing data that is all to be processed in a similar way. The Javascript Array data object gives the script author access to all of the array properties and methods when they work with arrays of information.

Array Object Syntax

Javascript CODE EXAMPLE
var array1 = ["John","Susan","Paul","Alice"];
var array2 = Array("John","Susan","Paul","Alice");
var array3 = new Array("John","Susan","Paul","Alice");

All of the approaches shown above will establish the same array in Javascript.

 


 

Array Types

Basic Array

Basic arrays represent one or more values separated by commas. The values can be a mixture of strings, numbers and variables. Each array item gets an index number associated with it which is its key. There are multiple ways to parse and access the elements of an array.

Build a basic array and access its items directly by their index number:

Javascript CODE EXAMPLE
var flavorArray = ["Vanilla","Chocolate","Strawberry","Orange"];

document.write(flavorArray[0]+" is very yummy.<br />");
document.write(flavorArray[1]+" is very yummy.<br />");
document.write(flavorArray[2]+" is very yummy.<br />");
document.write(flavorArray[3]+" is very yummy.");

Populate arrays by placing values directly into index positions of the array:

Javascript CODE EXAMPLE
var dogs = [];
dogs[0] = "Pitbull";
dogs[1] = "German Shepherd";
dogs[2] = "Greyhound";
document.write(dogs); // Pitbull,German Shepherd,Greyhound

Access array items dynamically using a for...in loop:

Javascript CODE EXAMPLE
var flavorArray = ["Vanilla","Chocolate","Strawberry","Orange"];

for (var flavor in flavorArray) {
    document.write(flavorArray[flavor]+" is very yummy.<br />");
}

 

Multideminsional Array

A multidimensional array is an array that is holding multiple arrays that can be of various types.

Javascript CODE EXAMPLE
var myArray = new Array(
          ["Vanilla","Chocolate","Strawberry"],
          ["Cat","Dog","Fox","Mouse","Owl"],
          ["HTML","CSS","Javascript","PHP"]
);
document.write(myArray[0]+"<br />");
document.write(myArray[1]+"<br />");
document.write(myArray[2]+"<br />");

Parse a multidimensional array dynamically using a for...in loop inside of a for loop:

Javascript CODE EXAMPLE
var myArray = new Array(
          ["Vanilla","Chocolate","Strawberry"],
          ["Cat","Dog","Fox","Mouse","Owl"],
          ["HTML","CSS","Javascript","PHP"]
);
for(var i = 0; i < myArray.length; i++) {
    document.write("<h3>Array | "+myArray[i].length+" items</h3>");
    for(var itm in myArray[i]) {
        document.write(myArray[i][itm]+"<br />");
    }
}

 


 

Methods of the Array Object

concat - returns a new array that is a concatenation of two or more arrays

Javascript CODE EXAMPLE
var fruits = ["Apple","Orange","Banana"];
var veggies = ["Celery","Onion"];
var dairy = ["Eggs","Cheese","Milk"];

var allFoods = fruits.concat(veggies,dairy);
document.write(allFoods);
// Apple,Orange,Banana,Celery,Onion,Eggs,Cheese,Milk

 

join - joins all array elements into a string, by a delimiter you specify

Javascript CODE EXAMPLE
var carMakers = ["Ford","Chevy","Dodge"];
var str = carMakers.join(" .:|:. ");
document.write(str); // Ford .:|:. Chevy .:|:. Dodge

 

push - add one or more elements onto the end of an array

Javascript CODE EXAMPLE
var gods = ["Jehovah","Allah"];
gods.push("Shiva","Buddha");
document.write(gods); // Jehovah,Allah,Shiva,Buddha

 

pop - takes the last element off the end of an array

Javascript CODE EXAMPLE
var monsters = ["Wolfman","Dracula","Frankenstein"];
lastMonster = monsters.pop();
document.write(lastMonster); // Frankenstein
document.write("<hr />");
document.write(monsters); // Wolfman,Dracula

 

shift - takes the first element off the beginning of an array

Javascript CODE EXAMPLE
var monsters = ["Wolfman","Dracula","Frankenstein"];
firstMonster = monsters.shift();
document.write(firstMonster); // Wolfman
document.write("<hr />");
document.write(monsters); // Dracula,Frankenstein

 

unshift - adds one or more elements to the beginning of an array

Javascript CODE EXAMPLE
var trees = ["Maple","Oak","Pine"];
trees.unshift("Holly","Spruce");
document.write(trees); // Holly,Spruce,Maple,Oak,Pine

 

slice - returns a portion of an array by specifying the starting index, and the range

Javascript CODE EXAMPLE
var people = ["Frank","Alice","John","Kim","Peter","Susan"];
var portion = people.slice(0,3); // starting from index 0, slice 3 elements
document.write(portion); // Frank,Alice,John

 

splice - remove array elements and optionally replace them

Javascript CODE EXAMPLE
var instruments = ["piano","guitar","harp","flute","cello"];
instruments.splice(0,2); // starting from index 0, remove 2 elements
document.write(instruments); // harp,flute,cello
Javascript CODE EXAMPLE
var instruments = ["piano","guitar","harp","flute","cello"];
instruments.splice(0,2,"organ","saxaphone"); // replace the first 2 elements
document.write(instruments); // organ,saxaphone,harp,flute,cello

 

reverse - reverse(transpose) an array

Javascript CODE EXAMPLE
var letters = ["a","b","c"];
letters.reverse();
document.write(letters); // c,b,a

 

sort - performs a natural sort on array elements

Javascript CODE EXAMPLE
var people = ["John","Robert","Alex"];
people.sort();
document.write(people); // Alex,John,Robert
Javascript CODE EXAMPLE
var myArray = [3,4,1,2];
myArray.sort();
document.write(myArray); // 1,2,3,4

 


 

Properties of the Array Object

length - returns the array length according to its internal pointer(not precise as a count in some instances). It can also remove array elements to a specified length. It is safe to use this property as a means of counting how many items are in an array, if you are sure the array will not have undefined or padded elements inside of it.

Javascript CODE EXAMPLE
var fruits = ["Apple","Orange"];
fruits[10] = "Banana";
document.write(fruits.length); // 11 (even though the array has only 3 items in it)
document.write("<hr />");
fruits.length = 0;
document.write(fruits.length); // 0 (shows empty array)

 

Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties
DevelopPHP.com is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2014 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at WebIntersect.com.
HTML5