Presentation is loading. Please wait.

Presentation is loading. Please wait.

ARRAYS MIT-AITI Copyright 2001.

Similar presentations


Presentation on theme: "ARRAYS MIT-AITI Copyright 2001."— Presentation transcript:

1 ARRAYS MIT-AITI Copyright 2001

2 Array A data structure that contains numbered pieces of data.
Different from associative arrays Arrays are really objects with added functionality

3 Array and Array Elements
An array stores numbered pieces of data Each numbered datum is called an element of the array and the number assigned to it an index. The elements of the array maybe of any type. You can even store arrays as elements in another array.

4 Creating Arrays Arrays are created using the Array() constructor or an Array literal The above constructor can be invoked in three ways. var a = new Array();//creates new empty array var a = new Array(5 , 4, “Wassup”);//You can specify elements of array var a = new Array(10);//You can specify length of array Var a = [[1,{x:1,y:2}], [2,{x:3,y:4}]];//Array literal

5 Reading and Writing Elements
Array elements are accessed using the [ ] operator. A reference to the array is on the left of the operator whilst a non-negative number is in the operator. value = a[7];

6 Adding Elements Add a new element by assigning a value to it.
Arrays in JavaScript are sparse: only elements in existence have memory allocated to them.

7 Array Length This property specifies how many elements an array has. This property is automatically updated whenever new elements are added to the array. a = new Array(1,2,3); a.length == 3 //Evaluates to true;

8 Array Length usage The length property enables us to loop through elements of an array; var girls = [“Sarah”, “Selma”, “Seme”, “Kate”]; for(var i=0;i < girls.length;i++) alert(girls[i]); If the array is not contiguous the above would change to for(var i=0;i < girls.length;i++) if girls[i]alert(girls[i]);

9 Removing elements To delete elements from an array set the length property so that those elements that you want to delete are out of the length range. If you use the delete property the element becomes undefined but the length of the array remains the same

10 Array Methods join() reverse() sort() concat() slice() toString()

11 join() Converts all the elements of an array to a string and concatenates them. You can specify an optional string used to separate the elements in the resulting string. var a = [1,2,3] a.join(“, ”);//s == “1, 2, 3”

12 reverse() Reverses the order of elements in an Array.
It doesn’t create a new array but rearranges the elements in the original array.

13 sort() Sorts the elements of an array
When called with no arguments sorts the arrays in alphabetical order. Optionally you could specify a function that would be used in sorting the elements

14 Concat Creates and returns an array that contains the elements of the original array that concat() was invoked on followed by the arguments to concat. var a = [1,2,3]; a.concat(4,5);//[1,2,3,4,5]

15 Splice() Returns a slice or subarray of the specified array.
Its two arguments specify the start and end of the slice to be returned. If only one argument is specified the slice returned contains all elements from the start position till end of the array


Download ppt "ARRAYS MIT-AITI Copyright 2001."

Similar presentations


Ads by Google