Arrays:
It is a collection of elements of homogenious or different data types.
It is a collection of elements of homogenious or different data types.
1.
Creating Array
a) var
n=[1,2,3,4,5];
var
months[“Jan”, “Feb”, 3, 4, “May”];
b) var
n=new array(1,2,3,4,5);
c) var
n=new array[5];
2.
Adding Elements : New element can be inserted at given position.
var
n=[1,2,3,4,5];
var
n[3]=6;
3.
Accessing Elements
Last
index is known by length attribute. So elements are accessed from beginning to
ending
by using a loop.
Ex :
for(var i=0;i<ar.length;i++)
{
document.write(ar[i]);
}
Array
indexes are zero-based, which means the first item is [0], second is [1], and
so on.
Array Properties:
Here is a list of each property and their description.
Property
|
Description
|
Returns a reference to the array function that created the
object.
|
|
index
|
The property represents the zero-based index of the match in the
string
|
input
|
This property is only present in arrays created by regular
expression matches.
|
Reflects the number of elements in an array.
|
|
The prototype property allows you to add properties and methods
to an object.
|
Array Methods
Here is a list of each method and its description.
Method
|
Description
|
Returns a new array comprised of this array joined with other
array(s) and/or value(s).
|
|
Returns true if every element in this array satisfies the
provided testing function.
|
|
Creates a new array with all of the elements of this array for
which the provided filtering function returns true.
|
|
Calls a function for each element in the array.
|
|
Returns the first (least) index of an element within the array
equal to the specified value, or -1 if none is found.
|
|
Joins all elements of an array into a string.
|
|
Returns the last (greatest) index of an element within the array
equal to the specified value, or -1 if none is found.
|
|
Creates a new array with the results of calling a provided
function on every element in this array.
|
|
Removes the last element from an array and returns that element.
|
|
Adds one or more elements to the end of an array and returns the
new length of the array.
|
|
Apply a function simultaneously against two values of the array
(from left-to-right) as to reduce it to a single value.
|
|
Apply a function simultaneously against two values of the array
(from right-to-left) as to reduce it to a single value.
|
|
Reverses the order of the elements of an array -- the first
becomes the last, and the last becomes the first.
|
|
Removes the first element from an array and returns that
element.
|
|
Extracts a section of an array and returns a new array.
|
|
Returns true if at least one element in this array satisfies the
provided testing function.
|
|
Represents the source code of an object
|
|
Sorts the elements of an array.
|
|
Adds and/or removes elements from an array.
|
|
Returns a string representing the array and its elements.
|
|
Adds one or more elements to the front of an array and returns
the new length of the array.
|
<!DOCTYPE
html>
<html>
<body>
<script>
var
i;//creating variable i of type number
var c = new
Array();//array creatin
//var c =
new Array[];// invalid due to [] brackets
/* array
initialisation*/
c[0] =
"idli";
c[1] =
"wada";
c[2] =
"dosa";
/* or we can
initialise
var c=new
Array("idli","wada","dosa");
(or)
var
c=["idli","wada","dosa"];
*/
for
(i=0;i<c.length;i++)
{
document.write(c[i]
+ "<br>");
}
</script>
</body>
</html>
Examples
<html>
<head>
<title>accessing array
elements</title>
</head>
<body>
<script
language="JavaScript">
// f=new Array(5);//creating array of size 5
/* f[0]="Rama";
f[1]="Sita";
f[2]="laxman";
f[3]="Bharat";
f[4]="Shathragna";*/
f=["Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true];//valid
//f={"Rama","Sita","laxman","Bharat","Shathragna"};//invalid
//var f=["Rama","Sita","laxman","Bharat","Shathragna"];//valid
//var
f=("Rama","Sita","laxman","Bharat","Shathragna");//invalid
//printing array elements
var n=f.length;
for(var
i=0;i<n;i++)
document.write(f[i]+"</br>");
var
j=f.join();//this method is used to print array elements in a single line
//
j=f.join();//VALID
document.write(j);
</script>
</body>
</html>
<!-Program
for Array http://improvejavascript.blogspot.in/-->
<html>
<head>
<script
type="text/javascript">
var
a=[1,2,3,4,5];
for(var
i=0;i<a.length;i++)
document.writeln("<H1>"+a[i]+"</H1>");
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<title>accessing array
elements</title>
</head>
<body>
<script
language="JavaScript">
var f=new
Array("Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true);
// var f=new
array("Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true);////invalid
due to small 'a'
//printing array elements
document.write(f[0]+"<br>");
document.write(f[1]+"<br>");
document.write(f[2]+"<br>");
document.write(f[3]+"<br>");
document.write(f[4]+"<br>");
j=f.join();//this method is used to print
array elements in a single line
document.write(j);
</script>
</body>
</html>
ExAMPLE:
<!
javascript array automatically extends its size-->
<html>
<head>
<title>accessing array
elements</title>
</head>
<body>
<script
language="JavaScript">
var f=new Array(2);
//var f=new
Array[2];//invalid due to [] brackes
f[0]="Rama";
f[1]=1//semicolon is optional
f[2]=3.2;
//printing array elements
document.write(f[0]+"<br>");
document.write(f[1]+"<br>");
document.write(f[2]+"<br>");
document.write(f[3]+"<br>");
document.write(f[4]+"<br>");
j=f.join();//this method is used to print
array elements in a single line
document.write(j);
</script>
</body>
</html>
********************************************************************
<html>
<head>
<title>accessing array
elements</title>
</head>
<body>
<script
language="JavaScript">
f=["Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true];//valid
//
f=("Rama","Sita","laxman","Bharat","Shathragna");//invalid
//var
f=["Rama","Sita","laxman","Bharat","Shathragna"];//valid
//var
f=("Rama","Sita","laxman","Bharat","Shathragna");//invalid
//printing array elements
document.write(f[0]+"<br>");
document.write(f[1]+"<br>");
document.write(f[2]+"<br>");
document.write(f[3]+"<br>");
document.write(f[4]+"<br>");
j=f.join();//this method is used to print
array elements in a single line
document.write(j);
</script>
</body>
</html>
Example:
search an element and remove it from the array
<!
searching an array-->
<html>
<head>
<title>searching a
element</title>
</head>
<body>
<script
language="JavaScript">
var
a=["Ram","kiran",34,36.54,"sita"];
//
a=["Ram","kiran",34,36.54,'sita',"sita"];//valid
//display the array
document.writeln("<p>");
var n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+ " ,");
}
document.writeln("</p>");
//ask the user to remove item
var r=prompt("Please type what you want
to remove"," ");
var temp=new Array(a.length-1);//we have to
remove one element so -1 is must else we get "undefined"
var m=0;
//for loop searches, and removes
for(var i=0;i<n;i++)
{
if(a[i]==r)
{
//does nothing
;
}
else
{
temp[m]=a[i];
m++;
}
}//end of for loop
//copying from array temp to array a
a=temp;
//display the new array
document.write("<p>");
n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+" ,");
}
document.writeln("</p>");
// document.close();
</script>
</body>
</html>
// same program using external program
function search()
{
a=["Ram","kiran",34,36.54,'sita'];//valid
//display the array
document.writeln("<p>");
var n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+ " ,");
}
document.writeln("</p>");
//ask the user to remove item
var r=prompt("Please type what you want to remove"," ");
var temp=new Array(a.length-1);
var m=0;
//for loop searches, and removes
for(var i=0;i<n;i++)
{
if(a[i]==r)
{
//does nothing
;
}
else
{
temp[m]=a[i];
m++;
}
// document.writeln(r+ "Not found");
}//end of for loop
//copying from array temp to array a
a=temp;
//display the new array
document.write("<p>");
n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+" ,");
}
document.writeln("</p>");
//document.close();
}
//save the above program example SearchJs.js
<! searching an array-->
<html>
<head>
<title>searching a element</title>
<script type="text/JavaScript" src="SearchJs.js">
</script>
</head>
<body>
<input type="button" onclick="search()" value="remove"/>
</body>
</html>
// same program using external program
function search()
{
a=["Ram","kiran",34,36.54,'sita'];//valid
//display the array
document.writeln("<p>");
var n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+ " ,");
}
document.writeln("</p>");
//ask the user to remove item
var r=prompt("Please type what you want to remove"," ");
var temp=new Array(a.length-1);
var m=0;
//for loop searches, and removes
for(var i=0;i<n;i++)
{
if(a[i]==r)
{
//does nothing
;
}
else
{
temp[m]=a[i];
m++;
}
// document.writeln(r+ "Not found");
}//end of for loop
//copying from array temp to array a
a=temp;
//display the new array
document.write("<p>");
n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+" ,");
}
document.writeln("</p>");
//document.close();
}
//save the above program example SearchJs.js
<! searching an array-->
<html>
<head>
<title>searching a element</title>
<script type="text/JavaScript" src="SearchJs.js">
</script>
</head>
<body>
<input type="button" onclick="search()" value="remove"/>
</body>
</html>
Array
Properties
rere
Property
|
Description
|
Constructor
|
Returns a reference to the array function that created the object
|
No comments:
Post a Comment