IT Community - Software Programming, Web Development and Technical Support

Manipulating in array

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > HTML, CSS and Javascript Coding Techniques

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-15-2008, 02:54 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-15-2008, 02:57 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-15-2008, 02:58 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-15-2008, 02:59 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-19-2008, 12:25 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-19-2008, 12:26 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-19-2008, 12:28 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-21-2008, 09:03 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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(";")
The value of the string jwords is:
Code:
limit;lines;finish;complete;In;Out
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-21-2008, 09:05 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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()
The value of the string lastword is:

Out
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-22-2008, 11:19 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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")
The array, words, will be:
Code:
limit, lines, finish, complete, In, Out
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 05-22-2008, 11:20 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

reverse() - Puts array elements in reverse order.
Code:
words = new Array("limit","lines","finish","complete","In","Out")
words.reverse()
The array, words, will be:
Code:
Out, In, complete, finish, lines, limit
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 05-22-2008, 11:21 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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()
The array, words, will be:
Code:
In, complete, finish, lines, limit
The string word will be:

Out
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 05-27-2008, 12:32 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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()
The value of words becomes:

In,Out,complete,finish,limit,lines
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 05-27-2008, 12:33 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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")
The value of words becomes:

limit, lines, finish, done, On, Out

The value of words1 is set to:

complete, In
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 05-27-2008, 12:34 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

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(";")
The values in the array swords is:

limit, lines, finish, complete, In, Out
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 06-02-2008, 03:08 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Manipulating in array

unshift() - Places element at the start of an array
Code:
words = new Array("finish","complete","In","Out")
word = words.shift("limit","lines")
The array, words, will be:
Code:
limit, lines,finish, complete, In, Out
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 05:48 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0