Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

Java Script Lecture-11


Topic: Methods of Array Object
a) Methods for Adding and Deleting an Array Element
i) push() : This method adds a new item(items ) to the end of an
array and returns the new length of an array.

ii) pop() : This method removes the last element from an array and
returns the value that was removed.

iii) unshift() : This method adds a new item(items) to the beginning


of an array and returns the new length of an array.

iv) shift() : Shifting is equivalent to popping, working on the first


element instead of the last. The shift() method removes the first
array element and shifts all other elements to a lower index. And
returns the element that was shift out.
(Note: You can pass multiple arguments to push and unshift method to add
multiple elements at a time)
Example
var a =[10,20,30,40];
console.log(a);
Output: [10,20,30,40]

console.log("After pushing 50 and 60 ");


a.push(50,60);
console.log(a);
Output: [10,20,30,40,50,60]

console.log("Popped value is : "+a.pop());


console.log(a);
Output: Popped value is : 60
[10,20,30,40,50]

Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

a.unshift(100,200);
console.log(a);
Output: [100,200,10,20,30,40,50]

a.shift();
console.log(a);
Output: [200,10,20,30,40,50]

b) Methods for sorting


 The sort() method sorts an array alphabetically. By default,
the sort() function sorts values as strings. This works well for strings
("Apple" comes before "Banana").

 However, if numbers are sorted as strings, "25" is bigger than "100",


because "2" is bigger than "1".

 Because of this, the sort() method will produce incorrect result when
sorting numbers. You can fix this by providing a compare function

The Compare Function :


 The purpose of the compare function is to define an alternative sort
order. The compare function should return a negative, zero, or positive
value, depending on the arguments:

function(a, b) {return a - b}

 When the sort() function compares two values, it sends the values to the
compare function, and sorts the values according to the returned
(negative, zero, positive) value.
i) If the result is negative a is sorted before b.
ii) If the result is positive b is sorted before a.

Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

iii) If the result is 0 no changes are done with the sort order of the two
values.

Example
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
console.log(points);
Output : [ 1,5,10,25,40,100]

Use the same trick to sort an array descending:


var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
console.log(points);
Output : [100,40,25,10,5,1]

c) Combining an Array elements into a String

 The JavaScript method toString() converts an array to a string of


(comma separated) array values.
Example
var points = [40, 100, 1, 5, 25, 10];
console.log(points.toString());

Output: 40,100,1,5,25,10

 The join() method also joins all array elements into a string. It behaves
just like toString(), but in addition you can specify the separator.
Example
var points = [40, 100, 1, 5, 25, 10]
console.log(points.join(' # '));

Output: 40#100#1#5#25#10

Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

d) Combining two or more array using concat() method:


 It is used to join two or more arrays. The existing array does not change
but returns a new array, containing the values of the joined arrays.
Syntax:
Array1.concat(others Arrays)
Example:
var a1=[10,20,30];
var a2=[40,50,60,70];
var a3 = a1.concat(a2);
console.log(a3);
Output: [10,20,30,40,50,60,70]

e) Creating parts of array using slice() method:


 The slice() method slices returns a section of an array.
The slice() method creates a new array. It does not remove any
elements from the source array.
Syntax:
Array.slice(start,[end]);
Where,
Start is required and end is optional
The slice() method can take two arguments like slice(1, 3).
The method then selects elements from the start argument, and up to
(but not including) the end argument.
Example
var a =[10,20,30,40,50,60];
var b = a.slice(3,5);
console.log(b);
Output : [40,50]

Note: if end occurs before start , no elements are copied to the new
array.

Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

f) Deleting elements using splice() :


Removes elements from an array and if necessary , inserts new
elements in their place, returning the deleted elements

var fruits = ["Banana", "Orange", "Apple", "Mango"];


fruits.splice(2, 0, "Lemon", "Kiwi");

The first parameter (2) defines the position where new elements should
be added (spliced in).

The second parameter (0) defines how many elements should


be removed.

The rest of the parameters(optional) ("Lemon" , "Kiwi") define the new


elements to be added.

The splice() method returns an array with the deleted items:

Example:

var fruits = ["Banana", "Orange", "Apple", "Mango"];


console.log("Before Splice : "+fruits);
Output : [“Banana”,”Orange”,”Apple”,”Mango”]

var t = fruits.splice(1,2,"Lemon","Kiwi");

console.log("After splice : "+ fruits);


Output : [“Banana”,”Lemon”,”Kiwi”,”Mango”]

console.log("Deleted fruits are : "+t);


Output : [”Orange”,”Apple”]

Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

g) Searching element in Array using indexOf() and lastIndexOf() :


indexOf () : returns the index of the first occurrence of a value in an
array otherwise returns -1

lastIndexOf() : returns the index of the last occurrence of a value in an


array otherwise returns -1

Example

var fruits = ["Banana", "Orange", "Banana", "Kiwi"];

console.log(fruits.indexOf("Banana"));
Output : 0

console.log(fruits.lastIndexOf("Banana"));
Output : 2

console.log(fruits.indexOf("Apple"));
Output : -1

h) Reversing an Array using reverse() :


Returns an array object with the element reversed.
Example
var a =[10,20,30,100];
console.log(a.reverse());

Output: [100,30,20,10]

Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like