Javascript Arrays Methods


Google
concat joins two or more array and returns a new array with all the elements of both arrays.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
 var peopleNames2 = new Array("Arslan Ameen","Hassan");
 var allPeopleNames = peopleNames.concat(peopleNames2);

indexOf returns the index number of specified element.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
var indexOfAbdullah = peopleNames.indexOf("Abdullah");

join creates a string of all the elements present in the array. It separate all the elements of array by comma by default. But, You can specify other things/characters within the parenthesis.

var peopleNames = new Array("Ahmed Bilal","Abdullah","Arslan Ameen");
var stringPeopleNames = peopleNames.join();

lastIndexOf returns the index of specific element/word from last. The difference between indexOf & lastIndexOf is that indexOf returns the index number of specific element from start and the lastIndexOf returns index number of element from end. After observing the following example it will be more clearer to you.

var peopleNames = new Array("Ahmed Bilal","Abdullah","Arslan Ameen","Bob Tabor","Arslan Ameen", "Scott Hanselman","Scott Hunter");
var lastIndexOfArslanAmeen = peopleNames.lastIndexOf("Arslan Ameen"); // returns 4
var IndexOfArslanAmeen = peopleNames.indexOf("Arslan Ameen"); // returns 2

pop removes and return the last element of Array.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
var lastElement = peopleNames.pop();

 

push added a new item to the end of Array and retruns the new length of Array.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
var lenghtOfArrayAfterAdding = peopleNames.push("Arslan Ameen");

 If you doesn’t understand the concept of Pop & Push click on the image stack

reverse is a method which reverses the original array.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
peopleNames.reverse(); // returns Abdullah, Ahmed Bilal

shift removes and returns the first item in the array.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
var removedItem = peopleNames.shift();

unshift adds the new item in the beginning of the array and returns the Length of Array after adding the item.

var peopleNames = new Array("Ahmed Bilal","Abdullah");
var LenghtAfterAdding = peopleNames.unshift("Arslan Ameen"); \\ 3

sort is a handy function which sort the array. It sort the original array.

var vehicles = new Array("Car","Bike","Aeroplane");
vehicles.sort(); // Aeroplane, Bike, Car

slice returns part of existing array. The slice method have two parameter start and end. Start is the index of element which you want to include and End is the index of element to till which you want to include. The element of end index is not included.

var peopleNames = new Array("Ahmed Bilal","Abdullah","Arslan Ameen","Bob Tabor","Arslan Ameen", "Scott Hanselman","Scott Hunter");
var somePeopleNames = people.slice(0,3); // Ahmed Bilal, Abdullah, Arslan Ameen

Note: Bob Tabor is not included in somePeopleNames

splice adds or remove items in array. Splice method has three parameter start, number of items, word . Start is the index from where you want to add or delete element. The number of items is the number you want to delete. If you specify 0 no item will be deleted. And in the last, you can add the items you want to add. It returns the removed items.

var peopleNames = new Array("Ahmed Bilal","Abdullah","Arslan Ameen","Bob Tabor","Arslan Ameen", "Scott Hanselman","Scott Hunter");
var someOtherPeoples = peopleNames.splice(0,3,"Rashid","Ammar");

Now, the peopleNames array become Ahmed Bilal, Abdullah, Arslan Ameen, Rashid, Ammar, Bob Tabor, Arslan Ameen, Scott Hanselman, Scott Hunter

toString return a string of all elements present in the array.

var peopleNames = new Array("Ahmed Bilal","Abdullah");

var peopleNamesString = peopleNames.toString();

 

Here is great explanation of toString, valueOf, join methods. I didn’t mention valueOf method because I myself didn’t understand it. Check this question, may be you understand the difference of toString, valueOf, join methods.

toString vs valueOf vs join

 

Advertisements

One thought on “Javascript Arrays Methods

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s