0% found this document useful (0 votes)
2 views10 pages

Array Methods

The document provides a comprehensive overview of various array methods in JavaScript, including their functionalities and examples of usage. Key methods covered include push, pop, shift, unshift, join, toString, flat, concat, reverse, slice, splice, indexOf, lastIndexOf, includes, sort, find, filter, and map. Each method is explained with syntax and examples to illustrate its application in manipulating arrays.

Uploaded by

mamatha071297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Array Methods

The document provides a comprehensive overview of various array methods in JavaScript, including their functionalities and examples of usage. Key methods covered include push, pop, shift, unshift, join, toString, flat, concat, reverse, slice, splice, indexOf, lastIndexOf, includes, sort, find, filter, and map. Each method is explained with syntax and examples to illustrate its application in manipulating arrays.

Uploaded by

mamatha071297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Vikram

Array Methods

1.push() 6.toString() 11.splice() 16.find() 21. some()


2.pop() 7.flat() 12.indexOf() 17. filter() 22. every()
3.shift() 8.concat() 13.lastIndexOf() 18. map()
4.unshift() 9.reverse() 14.includes() 19. forEach()
5.join() 10.slice() 15.sort() 20. reduce()

1.push (): Adds new elements to the end of an array, and returns the new length.
Ex:
let num = [1, 2, 3, 4, 5];
num.push(10,20,30);
console.log(num); //[1, 2, 3, 4, 5, 10, 20, 30]

2.pop (): Removes the last element of an array, and returns that element.
Ex:
let num = [1, 2, 3, 4, 5];
num.pop();
console.log(num); //[1, 2, 3, 4]
3.shift (): Removes the first element of an array, and returns that element.
Ex:
let num = [1, 2, 3, 4, 5];
num.shift();
console.log(num); //[2, 3, 4, 5]
4.unshift (): Adds new elements to the beginning of an array, and returns the new length.
Ex:
let num = [1, 2, 3, 4, 5];
num.unshift(10,20);
console.log(num); //[10, 20, 1, 2, 3, 4, 5]
5.jion (): Joins all elements of an array into a string.
Ex:
let num = [1, 2, 3, 4, 5];
let result = num.join();
console.log(result, typeof result); //1,2,3,4,5 string
To remove “,” from an array we use empty string (“”).
let num = [1, 2, 3, 4, 5];
let result = num.join("");
console.log(result); //12345

let num = [1, 2, 3, 4, 5];


let result = num.join("#");
console.log(result); //1#2#3#4#5
It returns new String.
It will not modify Original Array.
6.toString (): Converts an array to a string, and returns the result.
Ex:
let num = [1, 2, 3, 4, 5];
let result = num.toString("#");
console.log(result); //1,2,3,4,5
Vikram
let res = num.toString("");
console.log(res); //1,2,3,4,5

7.flat (): Array. flat() method flatten an array, That means it removes the nested array elements
and returns new singular array.
Ex-1: falt() without arguments
let num = [1,2,3,[4,5]];
let res = num.flat();
console.log(res); //[1, 2, 3, 4, 5]
console.log(num); //[1, 2, 3, Array(2)]
Ex-2:
let num = [1,2,3,[4,[5]]];
let res = num.flat();
console.log(res); //[1, 2, 3, 4, Array(1)]
console.log(num); //[1, 2, 3, Array(2)]
Ex-3: flat() with arguments
let num = [1,2,3,[4,[5]]];
let res = num.flat(2);
console.log(res); //[1, 2, 3, 4, 5]
console.log(num); //[1, 2, 3, Array(2)]
Ex-4:
let num = [1,2,3,[4,[5]],6];
let res = num.flat(3);
console.log(res); //[1, 2, 3, 4, 5, 6]
console.log(num); //[1, 2, 3, Array(2), 6]

8.concat (): It is used to merge multiple arrays and returns new array with the joined arrays.
Ex:
let str = ["A", "B", "C", "D", "E"];
let num = [1, 2, 3, 4, 5];
let x = num.concat(str);
console.log(x); //[1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E']
The return type of concat () method is new Array.

Ex:
let str = ["A", "B", "C", "D", "E"];
let num = [1, 2, 3, 4, 5];
let name = ["vicky","kiran"];
let x = num.concat(str,name);
console.log(x); //[1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'vicky', 'kiran']
9.reverse (): reverse () method reverses the order of the elements in an array.
Ex:
let res = num.reverse();
console.log(res); //[5, 4, 3, 2, 1]
console.log(num); //[5, 4, 3, 2, 1]
Reverse () method is changes or modifies the Original Array.
10.slice ():
Slice () method returns a new array according to its passing arguments start index and
end index respectively, the new Array is the sub-array of the original array.
Slice () method does not modify the original array.
Vikram
Syntax:
let result = Array.slice(start index, end index);
new Array
Ex-1: call by value
let num = [1,2,3,4,5];
let res = num.slice();
console.log(res);
res[1]=100;
console.log(res);
console.log(num);
console.log(res==num);
Ex-2: call by reference
let num = [1,2,3,4,5];
let num1 = num;
num1[3] = 300;
console.log(num);
console.log(num1);
console.log(num == num1);
Ex-3: with 1 argument
let num = [1,2,3,4,5];
let res = num.slice(2);
console.log(res); //[3, 4, 5]
Ex-4: with 2 arguments
let num = [1,2,3,4,5];
let res = num.slice(1,3);
console.log(res); // [2, 3]
11.splice ():
By using splice method, we can modify our array. (create, delete, update).
Splice() method changes the original array.

The return type of splice method is a new array with deleted items.

Ex-1: 0 Arguments
let num = [1,2,3,4,5];
let res = num.splice();
console.log(res); //[]
console.log(num); //[1, 2, 3, 4, 5]
Ex-2: 1 Argument
let num = [1,2,3,4,5];
let res = num.splice(2);
console.log(res); //[3, 4, 5]
console.log(num); //[1, 2]
Ex-3: 2 Arguments
let num = [1,2,3,4,5];
let res = num.splice(2,2);
console.log(res); //[3, 4]
console.log(num); //[1, 2, 5]
Vikram
Ex-4: 3 Arguments
let num = [1,2,3,4,5];
let res = num.splice(2,1,50,60,70);
console.log(res); //[3]
console.log(num); //[1, 2, 50, 60, 70, 4, 5]

let num = [1,2,3,4,5];


let res = num.splice(2,0,50,60,70);
console.log(res); //[]
console.log(num); //[1, 2, 50, 60, 70, 3, 4, 5]
Searching Methods:

12.indexOf ():
indexOf() method is used to search an element from the given array. indexOf() method
accepts two arguments. First argument is Element and second argument is starting
index.
indexOf() method based on the forward searching. It always starts searching from the
start index to last element of the array and gives the first matched elements index
number as the output.
If there is no matched element presented, it gives the result as -1.
Syntax:
arr.indexOf(searchElement, startIndex);
Ex:
let num =[1,2,3,4,5,6,2];
let res = num.indexOf(2);
console.log(res);//1

let res1 = num.indexOf(2,3);


console.log(res1);//6

let res2 = num.indexOf(7);


console.log(res2);//-1
13.lastIndexOf ():
lastIndexOf() method is used to search an element from the given array. lastIndexOf ()
method accepts two arguments. First argument is Element and second argument is
starting index.
lastIndexOf() method based on backward searching. It always starts searching from the
last element of an array to start index and it gives the first matched element index
number as the output.
If there is no matched element presented, it gives the result as -1.
Syntax:
arr.lastIndexOf(searchElement, startIndex);
Ex:
let num =[1,2,3,4,5,2];
let res = num.lastIndexOf(2);
console.log(res);//5

let res1 = num.lastIndexOf(2,3);


console.log(res1);//1
Vikram

let res2 = num.lastIndexOf(7);


console.log(res2);//-1

14.includes ():
includes() method returns a boolean data according to the present of the search
element in a given array.
It is based on forward searching.
It returns true if the element is available else it returns false.
Syntax:
arr.includes(searchElement, startIndex);
Ex: optional
let num =[1,2,3,4,5,6,2];
let res = num.includes(2);
console.log(res);//true

let res1 = num.includes(3,4);


console.log(res1);//false
15.sort () :
Array.sort() method sort one array in ascending order. It only gives expected output for
string type array. For number value array we are using sort method as a higher order
function, which accepts two parameters. The subtraction order of those parameters
defines ascending sorting and descending sorting.
It modifies the original array.
Syntax: arr.sort();
Ex:
let str = ["bq", "az", "ac", "kl", "mo", "dh"];
let res = str.sort();
console.log(res); //['ac', 'az', 'bq', 'dh', 'kl', 'mo']
console.log(str); //['ac', 'az', 'bq', 'dh', 'kl', 'mo']
let num = [5,6,10,22,1,30,47];
let res = num.sort();
console.log(res);//[1, 10, 22, 30, 47, 5, 6]
console.log(num);//[1, 10, 22, 30, 47, 5, 6]

//ascending order
let res1 = num.sort((a,b)=>{
return a-b;
})
console.log(res1);//[1, 5, 6, 10, 22, 30, 47]

//descending order
let res2 = num.sort((a,b)=>{
return b-a;
})
console.log(res2);//[47, 30, 22, 10, 6, 5, 1]

let res3 = num.sort((a,b)=>a-b);


console.log(res3);//[1, 5, 6, 10, 22, 30, 47]
Vikram
16.find ():
It is a high order function which accepts one callback function with three parameters i.e.,
element, index and arrayReference where index and arrayReference are optional.
Find() method always works on conditional expression.
The return type of find method is the first matched element, according to the given
condition. If the condition is false, find method result is undefined.
Syntax:
let res = arr.find(function(element, index, arrayReference){
return condition; optional
}
if true console.log(res); //first matched element
else console.log(res); //undefined
Ex:
let num = [1,2,3,4,5,2,1];
let res = num.find(function(ele,i,a){
return ele==2;
})
console.log(res); //2
let num = [1,2,3,4,5,2,1];
let res = num.find(function(ele,i,a){
return ele==7;
})
console.log(res); //undefined
let num = [1,5,8,9,6,7,3];
let res = num.find(function(ele,i,a){
return i==5;
})
console.log(res); //7

let num = [1,5,8,9,2,6,7,3];


let res = num.find(e=>e==2);
console.log(res);//2
17.filter ():
It is a high order function which accepts one callback function with three parameters i.e.,
element, index and arrayReference where index and arrayReference are optional.
filter () method works on Conditional Expression. The return type of filter() method is a
new array with all matched elements, according to the given condition.
filter() method does not change the original array. It returns a new sub-array of the
original array.
Syntax:
let res = arr.filter( (element, index, arrayReference)=>{
new [] return conditionalExpression; optional
});
Ex-1:
let num = [1,2,3,4,5,2,1];
let res = num.filter((ele,i,arr)=>{
return ele==1;
});
console.log(res); //[1, 1]
Vikram
Ex-2:
let objArr = [{ name: "K", age: 20 },
{ name: "V", age: 22 },
{ name: "B", age: 14 },
{ name: "Z", age: 18 }];

let res = objArr.filter(e=>{


// if(e.age>=18){
// return e;
// }
return e.age>=18;
})
console.log(res);
18.map ():
It is a high order function which accepts one callback function with three parameters i.e.,
element, index and arrayReference where index and arrayReference are optional.
Using map method, we can modify (create, update) given array according to the passing
expression result.
Map method returns a new array with updated elements.
Map method does not change the original array.
The length of returned array is always equal to the length of original array.
Syntax:
let res = arr.map( (element, index, arrayReference)=>{
new [] return expression; optional
new ref });

Ex-1:
let num = [1,2,3,4,5,2,1];
let res = num.map((e)=>{
return e+10;
})
console.log(res); //[11, 12, 13, 14, 15, 12, 11]

let res1 = num.map((e,i)=>{


console.log(e,i);
return e+i;
})
console.log(res1); //[1, 3, 5, 7, 9, 7, 7]

let res2 = num.map(function(ele){


if(ele==1) return ele;
})
console.log(res2);//[1, undefined, undefined, undefined, undefined, undefined, 1]

Ex-2:
let objArr = [{ name: "K", age: 20 },
{ name: "V", age: 22 },
{ name: "B", age: 14 },
{ name: "Z", age: 18 }];
Vikram
objArr = objArr.map((ele,i,arr)=>{
if(ele.age>=18){
ele.isEligible=true;
}else{
ele.isEligible=false;
}
return ele;
})
console.log(objArr);
19.forEach ():
forEach() method iterates the array elements from starting indexed last index.
forEach() method does not return anything. The return type of forEach() is undefined.
forEach() method we are using for looping purpose.
It will not modify the original array.
Syntax:
let res = arr.forEach( (element, index, arrayReference)=>{
undefined statements; optional
});
Ex-1:
let num = [1,2,3,4,5,6,2,1];
let num1 = [10,20];
num.forEach((ele,i,arr)=>{
num1.push(ele);
})
console.log(num1); //[10, 20, 1, 2, 3, 4, 5, 6, 2, 1]
Ex-2:
let num = [1,2,3,4 ];
let res = num.forEach((ele)=>{
console.log(ele);
})
console.log(res);
20.reduce ():
It is a higher order function which accepts two arguments, one callback function and
another one initial value (optional).
Callback function has four parameters accumulator, element, index and arrayReference
where index and arrayReference are optional.
Reduce method executes one reducer function for every element presented inside the
array.
The return type of reduce() method is a single value, which is the accumulator value.
Syntax: startElement nextElement
let res = arr.reduce((Accumulator, element, index, arrayReference)=>{
Accumulator value return Accumulator; optional
(single value) }, initialValue);
Ex-1: optional
let num = [1,2,3,4,5,6];
let sum = num.reduce((acc,ele)=>{
return acc+ele;
})
console.log(sum); //21
Vikram
Ex-2:
let num = [1,2,3,4,5,6];
let sum = num.reduce((acc,ele)=>{
return acc+ele;
},100)
console.log(sum); //121
Ex-3: To find maximum element in the array.
let num = [1,2,9,3,6,4,8];
let max = num.reduce((acc,ele)=>{
if(ele>acc){
return ele;
}else{
return acc;
}
})
console.log(max); //9
Ex-4: To find minimum element in the given array.
let num = [1,2,9,3,6,4,8];
let min = num.reduce((acc,ele)=>{
if(ele<acc){
return ele;
}else{
return acc;
}
})
console.log(min); //1
21.some ():
The some() method checks if any array elements pass a test (provided as a callback
function).
The some() method executes the callback function once for each array element.
The some() method returns true (and stops) if the function returns true for one of
the array elements.
The some() method returns false if the function returns false for all of the array
elements.
The some() method does not change the original array.
Syntax:
array.some(function(value, index, arr), this)
Ex:
let num = [20,60,15,18,24];
let res = num.some((e)=>{
console.log(e);
return e>=18;
})
console.log(res); //true

let num = [20,60,15,18,24];


let res = num.some((e)=>{
console.log(e);
return e>=70;
})
console.log(res); //false
Vikram
22.every ():
The every() method executes a function for each array element.
The every() method returns true if the function returns true for all elements.
The every() method returns false if the function returns false for one element.
The every() method does not change the original array
Syntax:
array.every(function(currentValue, index, arr), thisValue);
Ex:
let num = [20,60,15,18,24];
let res = num.every((e)=>{
return e>=18;
})
console.log(res); //false

let num = [20,60,15,18,24];


let res = num.every((e)=>{
return e>=10;
})
console.log(res); //true

You might also like