M3 Part3 JavaScript Arrays
M3 Part3 JavaScript Arrays
M3 Part3 JavaScript Arrays
JavaScript Arrays
Array Object
Creating Arrays
Indexed Arrays
Multidimensional Arrays
Iterating over an Array
Adding Element to an Array
Removing Elements from an Array
Associative Arrays
Arrays and Strings
Array Object
Arrays are reference types.
Array is a variable that holds multiple values.
In JavaScript all arrays are instances of global Array object
(wrapper class).
length is an important property of Array
Creating Arrays
Arrays can be created in two ways:
Using constructors of Array (There are 3 versions of constructors)
Creating using array literal
Creating Arrays - Constructors
obj = new Array();
obj = new Array(arraysize);
obj=new Array(elements,…);
Examples:
var arr=new Array();
var arr=new Array(10);
var arr=new Array(“Mon”,”Tue”,”wed”);
Creating Arrays - Literals
var a=[“Mon”,”Tue”,”Wed”];
var b=[1,2,3,true,[“Mon”,”Tue”]];
Indexed Arrays
Normal arrays are indexed.
The index starts from zero.
Each item can be accessed based on its position or index in the
array.
Indexed Arrays
var arr = [“Kevin”, “Eric” , “Jacob” , 20 , true];
document.write(arr[0]); //Kevin
document.write(arr[1]); //Eric
document.write(arr[2]); //Jacob
document.write(arr[3]); //20
document.write(arr[4); //true
Multidimensional Arrays
Also called as jagged arrays.
They are array of arrays.
var student = [[“Kevin”,90,”CSE”] , [“Eric”,40,”IT”] ,
[“Jacob”,60,”ECE”]];
document.write(student[1][0]); //Eric
document.write(student[2][2]); //ECE
Detecting Arrays
Iterating Over an Array
Adding Element to an Array
Using array index
Using methods of Array
push – Append an item to an array
unshift – Inserts item in the beginning of the array
Adding Element to an Array –
Using Index
Adding Element to an Array –
Using push() and unshift()
Adding Element to an Array –
Using push() and unshift()
Combining Arrays – Using
concat()
Removing Elements from an
Array
Manipulating the length of the array
If the last element needs to be deleted decrement the length of the
array by 1.
Using methods of Array
pop – Deletes the last element of the array
shift – Deletes the first element of the array
Removing Elements from an
Array – Decrementing length
Removing Elements from an
Array – pop() and shift()
Editing Elements of an Array
Array object provides the following methods to edit arrays
Array.slice(beginindex,[endindex])
Returns an array from the begin index to end index
If endindex not specified returns till the end of the array
If the index is negative perform slicing in reverse
Does not modify original array
Array.splice(index,deletecount,[element0,…,elementn])
Returns the array by extracting elements from index to deletecount
deletecount – is not index but the number of elements to be deleted from the index
Optional elements are the elements to be replaced in the positions where the original
elements were removed
Modifies the original array
Extracting Elements of an Array –
slice()
Editing an Array – splice()
Associative Arrays
Associative arrays are arrays with key/value pairs.
It does not have numeric index.
The keys are used to access or modify the values.
var stud={“regno”:”19MIA1234”,”name”:”kevin”}
regno and name are keys
19MIA1234 and Kevin are respective values
Associative Arrays
Associative arrays do not support length property.
Hence we use a for..in loop
Arrays as Reference Types
Arrays are reference types.
Hence if we create an array the variable holds the reference or
memory address of that array.
If we copy this array the new array will also hold the same address.
Hence any changes done in the new array will affect the original
array and vice versa.
Arrays as Reference Types
Arrays and Strings
String.split(); - Returns an array by splitting the string into tokens
based on delimiter
Array.join(); - joins the elements of an array by placing the
delimiters alternative to elements
split() and join()
REFERENCE
Alexei White, JavaScript Programmer’s Reference, Wrox, ISBN:978-
81-265-2363-4