ARRAY METHODS :
- Array flat() :
This method is used to combine multiple array into a single array. It does not affect the original array.
const num = [[1,2],[3,4],[5,6]];
const num1=num.flat();
console.log(num1);
output:
[ 1, 2, 3, 4, 5, 6 ]
- Array flatmap() :
In this flatmap() method it do first map method functionality then it will combine the array into a single array.
map - to call a function on a every element in an array then it create a new array and return it
flat - this method is used to combine a multiple array into a single array.
//this is only a map method
const num2 = [1,2,3,4];
const num3=num2.map(add);
function add(i){
return [i,i+2];
}
console.log(num3);
output:
[ [ 1, 3 ], [ 2, 4 ], [ 3, 5 ], [ 4, 6 ] ]
//this is flatmap method
const num2 = [1,2,3,4];
const num3=num2.flatMap(add);
function add(i){
return [i,i+2];
}
console.log(num3);
output:
[
1, 3, 2, 4,
3, 5, 4, 6
]
- Array filter() :
This method uses the condition to store the element in the new array if the condition is pass only . it does not affect the original array.
const num4 =[10,2,30,4,5];
const num5 = num4.filter(fil);
function fil(age){
return age>=18;
}
console.log(num5);
output:
[ 30 ]
DIFFERENCE BETWEEN MAP() AND FILTER()
Map() - It return only a true or false but check all the condition. if we
give the condition in the map method means it give the true or
false.
It check all the element in the array and print if the condition
pass means it print true otherwise it does not pass the condition
means it print false
const num6 = [40,50,2,3,10];
const greater=function(i){
return (i>10);
}
const num7 = num6.map(greater);
console.log(num7);
output:
[ true, true, false, false, false ]
Filter() - This method check all the element if the each element pass the
condition means it will store in the new array.
It check all the element and print the element is pass the
condition.
const num8 = [40,50,2,3,10];
const greater1=function(i){
return (i>10);
}
const num9 = num8.filter(greater1);
console.log(num9);
output:
[ 40, 50 ]
4.Array reduce() :
The reduce() method runs a function on each array element to produce a single value.
The reduce() method works from left-to-right in the array.
This method takes 4 arguments
total
value
index
array
In this method the total take the first element in the array and value will take the second element in the array . and when we add the total and value it will give the singal value that value will take as a total and again value will update to the next element like wise it will continue until the array end is reach.
const num10 = [5,20,10,3];
const num11 = num10.reduce(red);
function red(total,value){
console.log("total====>"+ total);
console.log("value====>"+ value);
return total +value;
}
console.log(num11);
output:
total====>5
value====>20
total====>25
value====>10
total====>35
value====>3
38
5.Array every():
This method check all the element in the array , if all the element pass the condition means it give the true or false.
const num12=[1,2,3,4,5];
const num13 = num12.every(ever);
function ever(x){
return x>2;
}
console.log(num13);
output:
false
- Array some()
This method check all the element in the array same like every method but if any one condition pass means it print true otherwise false.
const num14 = [2,3,4,5,6];
const num15 = num14.some(i=>i<=2);
console.log(num15);
output:
true
7.Array Keys():
This method gives the array iterator object with key of an array
8.Array Entries():
The Key & Value pair is called entries.
9.Array with():
This method doesnot disturb the original array we can add the element to the array but it does not distrub the original array.
const num18 = [5,6,7,8];
const num19 = num18.with(0,4);
console.log(num19);
output:
[ 4, 6, 7, 8 ]
- spread():
This method is used for spread the array into a single array.
const num20=[1,2,3];
const num21=[4,5,6];
const res = [...num20,...num21];
console.log(res);
output:
[ 1, 2, 3, 4, 5, 6 ]
11.rest():
This method is used to store the array value in a single variable
Top comments (0)