Array

Arrays are used for compartmentalizing data, a data type that stores one or more pieces of data. The JavaScript Array object gives the script author access to all of the array properties and methods when they work with arrays of information.

All of the syntax approaches shown below will establish the same array:

var arr = ["a","b","c","d"]; var arr = Array("a","b","c","d"); var arr = new Array("a","b","c","d");

 

 

Properties

 

 

length – returns the total count of elements in the array. It also removes array elements to a specified length.

Methods

average() – computes the average value from an array of numbers.

 

concat() – returns a new array that is a concatenation of two or more arrays.

 

every() – determine whether or not all elements pass an evaluation.

 

filter() – create a new array of all elements that return true through a filter function.

 

isArray() – determine whether or not the specified data is an array.

 

join() – joins all array elements into a string, by a delimiter you specify.

 

push() – add one or more elements onto the end of an array.

 

pop() – remove the last element in an array and returns it.

 

shift() – remove the first element in an array and returns it.

 

shuffle() – shuffle an array according to the Fisher-Yates Shuffle Modern Algorithm.

 

slice() – returns a portion of an array.

 

some() – determine whether or not at least one element passes an evaluation.

 

splice() – remove array elements and optionally replace them.

 

reduce() – … reverse() – reverse array elements. sort() – sort array elements. unshift() – add one or more elements to the start of an array.

 

 

Array Types

 

 

Note: The first element in an array occupies the [0] index. The next element occupies the [1] index and so on. Arrays begin with a 0 index by default, be sure to keep that in mind when working with them. Referencing an array index number returns the data and can also assign/adjust the data at that index.

 

 

Basic Array

Basic arrays represent one or more pieces of data separated by commas. The data placed in an array may be a mixture of various objects like Strings, Numbers, Boolean values, Arrays, Objects, Etc…

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

var flavors = ["Vanilla","Chocolate","Strawberry","Orange"];

 

 

document.write(flavors[0]+" is very yummy.<br>");

 

document.write(flavors[1]+" is very yummy.<br>");

 

document.write(flavors[2]+" is very yummy.<br>");

 

document.write(flavors[3]+" is very yummy.");

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

var dogs = []; /* create an empty array */ dogs[0] = "Pitbull"; dogs[1] = "German Shepherd"; dogs[2] = "Greyhound"; document.write(dogs); /* Pitbull,German Shepherd,Greyhound */

Iterate over an array using a for…in loop:

var flavors = ["Vanilla","Chocolate","Strawberry","Orange"]; for ( var flavor in flavors ) { document.write(flavors[flavor]+" is very yummy.<br>"); }

Multideminsional Array

A multidimensional array is an array that is holding multiple arrays. Arrays can be deeply nested this way.

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] );

 

alert( myArray[1][2] ); /* Fox */

Parse a multidimensional array using a for…in loop inside of a for loop:

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>"); } }

 

 

Associative Array

 

 

If you are coming from a programming background and are looking for Associative Array information for JavaScript, this paragraph is included for you. Unfortunately JavaScript does not have syntax to create associative arrays that can directly access the Array object interface. In JavaScript the data types most like an associative array are Object objects or JSON objects.

var obj1 = { user:"John", age:22, country:"United States" }; document.write(obj1.user+" lives in "+obj1.country);

renewdev

I Am Developer And Want To Share My Knowledge With The World..:) ;) . I Have Another Social Networking Website (Renewchat) And For Designing Or Developing Of Website Purpose You Can Contact Me On Renew Coder .. :) ;)

Leave a Reply

Your email address will not be published. Required fields are marked *