This is a discussion on Manipulating in array within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; Hi, Arrays in JavaScript are extremely useful for storing and manipulating information you have coded directly into the script, or ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, Arrays in JavaScript are extremely useful for storing and manipulating information you have coded directly into the script, or information collected from the browser. The array is one of a number of objects built directly into JavaScript. Think of them simply as variables containing multiple values. They can hold string or numerical values, have no maximum boundaries (although the speed at which your scripts are interpreted will decrease as the amount of data increases) and are comma delimited. They have no persistence, meaning that their values are not held once the page containing the script has closed or reloaded.
__________________ With, J. Jeyaseelan Everything Possible |
| Sponsored Links |
| |||
| Single Dimensioned Arrays A simple example of a single-dimensioned array is: myArray = new Array(15,89,4,61,5) Which simply says create an array called myArray with five values, the values are integers and equal 15, 89, 4, 61 and 5. This information, were it to mean anything, could then be manipulated in all kinds of ways; you could print the results on the page using the simple command: document.write(myArray[1]) That would display 15 on the page. You could also perform simple math, were the need to arise: var mySum = myArray[1] * myArray[2] document.write(mySum) which displays the number 356. An important property of the array object is the length property; the extremely basic numerical array above has a length of five. Each of the values in the array can be accessed using their index within square brackets following the array name, for example: myArray[2] It’s important to remember that the indices of any array always begin with 0 representing the first item in the array; therefore, while the length property of this very simple array is five, the highest index is four.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Multidimensioned Arrays In the same way that arrays contain integers or strings as values, multidimensional arrays contain arrays as their values. The syntax for a multidimensional array is as follows: myArray = new Array([2,3,4], [5,5,9], [8,6,1]) To display the first item of the first array (which has an index of 0,0) you would need to use: document.write(myArray[0][0]) This is known as a two-dimensional array, even though there are three arrays each with three values, as the highest index is 2,2. Referencing each of the items in multidimensional arrays can be cumbersome and confusing, and I have found it easiest to keep arrays singular and just have more of them as necessary, i.e. have three separate arrays rather than one two-dimensional array.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Uses for Arrays To show you how useful arrays are when you need to store and retrieve information, I have put together an example that demonstrates their power and usefulness. Imagine that you have an online store selling computer components, and you offer five products out of five of your product lines as special offers every week. Obviously there is only so much information you can fit onto your homepage, so you can’t show all 25 of the week’s special offers at once. But because you want to alert people that these offers exist, you need something on the homepage. You decide that a random image generator would be your best use of space, and you want it to display one offer from each of the product lines at a time. You want each of the images to be a roll-over, the staple of any wholesome website. Each of the images should also link to a new page, giving the full description and details of each of the products. All of a sudden there is an incredible amount of information that your script is going to need to use; an array is going to be needed to keep track of it -- several arrays in fact.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| chop() - Used to truncate the last character of a all strings that are part of an array. This method is not defined so it must be written and included in your code. var exclamations = new Array("Look out!", "Duck!" ) exclamations.chop() Causes the values of exclamations to become: Code: Look out Duck
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| concat() - Concatenates either plain values or another array with the existing array, and returns the new array. Does NOT alter the original array. If the values to concat are strings or numbers, their actual values are added to the returned array. If the values to concat are object references, the same object reference will be added, and not the object itself. This means that both the old and new array will now contain references to those object(s), with changes to the referenced object affecting both arrays.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| grep(searchstring) - Takes an array and returns those array element strings that contain matching strings. This method is not defined so it must be written and included in your code. words = new Array("limit","lines","finish","complete","In","Ou t") inwords = words.grep("in") The array, inwords, will be: Code: lines, finish
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| join(delimiter) - Puts all elements in the array into a string, separating each element with the specified delimiter. Code: words = new Array("limit","lines","finish","complete","In","Out")
var jwords = words.join(";") Code: limit;lines;finish;complete;In;Out
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| pop() - Pops the last string off the array and returns it. This method is not defined so it must be written and included in your code. Code: words = new Array("limit","lines","finish","complete","In","Out")
var lastword = words.pop() Out
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| push(strings) - Strings are placed at the end of the array. This method is not defined so it must be written and included in your code. Code: words = new Array("limit","lines","finish")
words.push("complete","In","Out") Code: limit, lines, finish, complete, In, Out
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| reverse() - Puts array elements in reverse order. Code: words = new Array("limit","lines","finish","complete","In","Out")
words.reverse() Code: Out, In, complete, finish, lines, limit
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| shift() - Decreases array element size by one by shifting the first element off the array and returning it. This method is not defined so it must be written and included in your code. Code: words = new Array("limit","lines","finish","complete","In","Out")
word = words.shift() Code: In, complete, finish, lines, limit Out
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| sort() - Sorts the array elements in dictionary order or using a compare function passed to the method. Code: words = new Array("limit","lines","finish","complete","In","Out")
word = words.sort() In,Out,complete,finish,limit,lines
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| splice() - It is used to take elements out of an array and replace them with those specified. In the below example the element starting at element 3 is removed, two of them are removed and replaced with the specified strings. The value returned are those values that are replaced. This method is not defined so it must be written and included in your code. Code: words = new Array("limit","lines","finish","complete","In","Out")
words1 = words.splice(3, 2, "done", "On") limit, lines, finish, done, On, Out The value of words1 is set to: complete, In
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| split(deliimiter) - Splits a string using the delimiter and returns an array. Code: words = new String("limit;lines;finish;complete;In;Out")
var swords = words.split(";") limit, lines, finish, complete, In, Out
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| unshift() - Places element at the start of an array Code: words = new Array("finish","complete","In","Out")
word = words.shift("limit","lines") Code: limit, lines,finish, complete, In, Out
__________________ With, J. Jeyaseelan Everything Possible |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Manipulating scrollbar colors using CSS and JavaScript | santhakumar | HTML, CSS and Javascript Coding Techniques | 1 | 11-27-2007 05:55 AM |
| Three dimensional array | geoblow | Flash Actionscript Programming | 1 | 09-21-2007 03:47 AM |
| What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? | Archer | C# Programming | 2 | 08-25-2007 02:00 AM |
| When to you use $_REQUEST[ ] array in php..? | oxygen | PHP Programming | 2 | 07-30-2007 03:52 AM |
| Array filtering | Jeyaseelansarc | PHP Programming | 1 | 07-17-2007 08:07 AM |