Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

array object

Array object

Uploaded by

Isha Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

array object

Array object

Uploaded by

Isha Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

that ball2.

giY le webpage in a web


image will rotate around the browser you will get the desired output. You will see
18.12 ARRAY QBJECT ball.gif image.
ARAPray
data. Unlike
other
object is built-in object
used for storing and manipulating ordered
youcan
can
using. Array()
programming languages,.
alsohave amix of
different data JavaScript collecion
array entries may be of any data
types in the same array. type
of
and
are constructor or using array literals. array. Array may be declared either
The different ways of
var arr
new Array
creating: arrays in JavaScript
(
var arr =
new Array ); ;
var arr =
new Array (size)
0(element0, element1,
Var
arr=
The first way
[element element1, elementN-1) }:
mentioned above is used for creating an empty elementN-1l;
var 1ink = array. For example :
neW Array );
It creates a neW array
called link. Each element of the array is
name of the array followed by the
number of the element to be accessed by using the
number known as IndexX.In JavaScript, Index
is accessed square brackets. This
in
array willbe accessed as link [0]. number starts from 0. So, the first element of link
sO on. In order to assign some Similarly, the second element will be accessed as link [1] and
value to the first element of the link array, we
link [0] write,
"first.html ";
In the sinmilar way you can assign values to
other elemnents of an array.
link [1] = "second.html "
link [2] "third.html "
Similarly, link array can alsobe declared as,
var link = new Array (3)

JavaScript-II 477
var link = new Array ("first.html ", "Becond.html ",
var llnk = tElret.html n, "second. html "
"third.html
var link =(x,Y, z] t / /Here x="flret . html ",y= "second.htmi "and nthird.h"third,
"1;
z tmlu ):

NOTE: Accessing an array element whose value is not specificd will return undefined value.
Some of the commonly used properties and methods associated with Array object :
are
PROPERTY DESCRIPTION

length Provides a count of number of entries stored in an array.


Table 18.17 List of Properties of Array object
METHOD DESCRIPTION

concat (array 1 Combines the current array object with one or mc


more array
objects into a
Larray2[..arrayN']) single array.
join(['"'separator") Joins all element of an array as a single string
separated by a 'separato
if present or by comma if no separator is specified.
pop) Returns the value of last item in an array and removes it from array
push(iteml[,item2 Appends one or more items to the end of an array and returns its nesy
L,..itemN]|]) length.
reverse( ) Reverses the order of elements in the array. It also returns a copy of the
reversed array.
shift( ) Returns the value of first element in an array and removes it.
slice(begin[,end]) Creates a new array fromn an existing array such that new array is defined
by start and end indexes.
sort([fnamel) Sorts the elements' of an array either by ASCII values or according to
function of your choice.
splice(Index, how Removes one or more contiguous elements from an array and inserts
[,item 2,...itemNJ]|]) newelements at their places optionally.
tostring() Returns comma delimited string of values.
unshift(item 1, item2 Inserts one or mnore itemns to the beginning of array.
L.itemn]|)
Table 18.18 List of methods of Array object
You can access the properties and methods of Array object by specifying the oO]
name Array followed by a period ()and then the property or method name. So it is gen
written as,
ArraY.proerty_ name
ArraY.method ([argl arg2.argN])
Inaddition to one dímensional array, JavaScript also supports multi dimensional a
Multídimensional array is an array whose element are itself an array. For example,
478
JavaScript-lI
6,711:
atrix =[[1,2, 31, [5,
Here, matrixxis a 2-D array whose elements are matrix[0j[0]=1, matrix[0][1]=2,
matrix[1][2]=7.
PROGRAM16: Script to show accessing of array elements.

khead><titles AcceSsing Array


chtml> «/title</heads
kbody>eh3> Array Elementg :</h3>
(script language "JavaScript"s

var pic = ["img1gif"img2.gif", "img3.gif", "img4.gi£"]:


for (i = 0; i <pic, length ; i++)
document.write ("pic ["+i+ "] ="+ pic [i] +"<br>"):

</script>
</body>
</html>

On viewing the webpage in a web browser you will get the desired output.
Program 17 : Script to display multiple messages one at a time after specific interval
such that when you click the button, a link corresponding to that message
is displayed.
<html>
<head><title>Links and Arrays </title></head>
<body>
<table border = "0">
<tr><td>
<formn name "£rm">
<textarea rows = "5" name = "info" cols= "30"></textarea><br>
<input type = " button" value - "Click to get more" onclick =
"window. location = msg [current ]">
</form>
</tâs</tr>
</table>
<8cript
<!-
language = "JavaScript">

var current =-1;


var msg = new Array ()7
msg [01 TO search all typeg of information on internet";
nsg 1] = nyisit microsoft site for latest products";
sg (21 = "To download latest softwares";
var link - new Array ( ) i
1ink IO] = "http:/ /www.google, com" ;

JavaScript-I 479
1ink [1] = "http://www.microsoft.
1ink [2] - "http:/ /www.download.com":
function showmsg ()
{
if (current <msg.length-1)
Current ++;
else
Current 0;
ocument. frm. info.value = msg [currentl;
setTimeout ("showmsg() ", 2000):
}
showmsg ( )i

</script>
</body>
</html>

P
On viewing the webpage in aweb browser, you will be displayed three different messegs
at aregular intervals in atext box, on clicking the button, the corresponding link willopen.
number
ASSOCIATIVE
array andthe text ARRAY
18.18
instead
and canAssociative
var
persOn persOn
person the be
of
valuedefinednumbers.
disadvantage that is
["age"] [" arrays
can
name as
benew = The are
"]
assigned
= similiar
main
40;
Array );(
" advantage
Ankur" ; as youcannot to
regular

of
array
access
using
with
values that it
the
the
difference
using
string
the
numbers are that
easier the
index
in to
rememberthan
Associ
loop.a a value

ront

You might also like