Thursday, December 27, 2012

Array object


List of array methods

1.toString()
2.join()
3.sort()
4.reverse()
5.concat()
6.push()
7.pop()
8.shift()
9.unshift()
10.slice()
11.splice()

toString() is used convert the array element into string separated by comas. for example

var x=new Array("apples","orange","grapes");
document.write(x.toString()); //output is apples,orange,grapes

join() is used to combine each element in the array with given parameter, for example.

var x=new Array('a','s','d','f','g');
document.write(x.join('*'));//output is a*s*d*f*g

sort() is used to sort the array in ascending order. for example

var y=new Array('a','s','d','f','g');
document.write(y.sort());//output is a,d,f,g,s

consider another example

var y=new Array(10,30,45,23,78,1,23);
document.write(y.sort());//output would be 1,10,23,23,30,45,78

If you consider the same array y and wish to reverse the order hen we cannot use reverse function instead we need to use the below function.

var y=new Array(10,30,45,23,78,1,23);
document.write(y.sort(function(a,b){return b-a;}));//output would be 78,45,30,23,23,10,1

For more details refer -Sort function

concat() is to combine arrays, for example

var x=new Array(10,20,78,1,3);
var y=new Array('a','g','r');
document.write(x.concat(y));//output is 10,20,78,1,3,a,g,r

pop()- pop function is used to delete the last element in the array and output the deleted element.
for example

var x=new Array(10,20,30,40);
document.write(x.pop());//this would delete 40 and output 40;

shift() - Similar like pop function, shift is used to delete the first element in the array and output the deleted element.for example

var x=new Array(10,20,30,40);
document.write(x.shift());//this would delete 10 and output 10;

push() - Shift method is used to add an element at the end of the array and return the length of the array.
for example


var x=new Array(10,20,30,40);
document.write(x.push(99));//and the output would be the length of the new array which is 5, which indicates that 99 has been added as the last element in the array.

unshift()- unshift method is used to add an element to the beginning of the array and return the length of the array.for example


var x=new Array(10,20,30,40);
document.write(x.push(99));//and the output would be the length of the new array which is 5, which indicates that 99 has been added as the first element in the array.



slice()- slice method is used to retrieve a part of the array.
for example


var x=new Array(10,20,30,40);
document.write(x.slice(1,3));//output would be 20,30

splice() - Splice method performs two function one is inserting and deleting elements from the array.


for example

var x=new Array(10,20,70,40);
document.write(x.splice(2,1,30));//output is 70
document.write(x.toString());//output is 10,20,30,40

explanation
(x.splice(2,1,30)) - this indicates that it add 30 to the 2 nd position and delete 1(one) element from the 2nd position.



Hope this post was useful,will get back with some more post.


Informative Javascript

No comments:

Post a Comment