Javascript Array OverviewAn javascript array is an ordered set of values grouped together under a single variable name created by using an Array object constructor. You can create an Array literal by specifying the name of the array and the values of all its elements. Description: A collection of objects. What creates it: arrayName = new Array(arrayLength) or arrayName =new Array(element0, element1, . . ., elementN) How to access it: arrayName[i] Properties: constructor, index, input, length, prototype Methods: concat(), join(), length(), pop(), push(), reverse(), shift(), slice(), sort(), splice(), toSource(), toString(), unshift(), valueOf() Event handlers: None
Construct a basic Javascript array var preloadArray = new Array(); Construct a basic Javascript array with the certian values// Create an array and fill it with 12 values. var months = new Array(“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,“Aug”,”Sep”,”Oct”,”Nov”,”Dec”) Construct a basic Javascript array with the number of elements.JavaScript arrays begin with 0, not 1 egg = new Array(8) Array An ordered collection. For example: var animals = new Array(“cat”, “dog”, “mouse”) // load array var firstAnimal = animals[0] // access first array element var secondAnimal = animals[1] // access second element var thirdAnimal = animals[2] // access third element Sorting a JavaScript ArrayArray sorting is performed in ascending order alphabetically or numerically as appropriate Create a variable named myArray, and initialize it as a new array with four elements: var sortArray = new Array(4); Assign values to the four elements: myArray[0] = “x”; myArray[1] = “c”; myArray[2] = “d”; myArray[3] = “a”; Use the document.write method and the sort method to output the sorted list of elements,see below code <script language=”JavaScript”> <!-- var myArray = new Array(4); myArray[0] = “x”; myArray[1] = “c”; myArray[2] = “d”; myArray[3] = “a”; document.write(myArray.sort()); // --> </script>
JavaScript Array join() method tutorialThe javascript array join() method returns a string containing the elements of the array in the order they held within their element list, separated by the first argument of the method.Using the join() method to put all elements in the arra y into a string,separating each element with the specified delimiter. For Example words = new Array("IBM","SUN","MS") var ism = words.join("-") Then the value of the string ism is: IBM-SUN-MS JavaScript Array pop() method Using the pop() method to pop the last string off the array and returns it. The last element is removed from the array. For Example cm = new Array("IBM","SUN","MS") var popword = cm.pop() The value of the string popword is: MS JavaScript Array push() methodUsing javascript array push() method to append the arguments provided in the method to the end of the array. For Netscape 4.00–4.05, this method returns the last new element of the array. For Netscape 4.06+ and Internet Explorer 5.5, it returns the new length property of the array. For Example function push_demo() { var i = 0 for (i = 0; i < arguments.length; i++) { this[this.length] = arguments[i] } return this.length } if (typeof Array.prototype.push == "undefined") { Array.prototype.push = push_demo } Using reverse() to ut array elements in reverse order. cn = new Array("IBM","SUN","MS") cn.reverse() THe cn is MS,SUN,IBM Array shift() method Using shift() method to extract the first element in the array object's element list and returns it. The element returned is removed from the array altogether. For Example function Array_shift() { var i = 0 var response = this[0] for (i = 0; i < this.length-1; i++) { this[i] = this[i + 1] } this.length-- return response } if (typeof Array.prototype.shift == "undefined") { Array_prototype.shift = Array_shift } Using the concat() method to join two arrays. Example: var cm = new cmay(2) cm[0] = "IBM" cm[1] = "SUN" var cn = new cmay(2) cn[0] = "MS" cn[1] = "APPLE" document.write(cm.concat(cn)) |