What is Iteration?
It is a repeating process,usually with the aim of approaching a desired result.
filter()
creating a new array containing only the element that pass a specific condition.
let a=["apple","grap","banana","watermelon"];
let b=a.filter((x)=>(x.length>6));
console.log(b);
watermelon
flatMap()
first map all the element in the array,Then create new array.
let a=[2,7,3,1,0];
let b =a.flatMap((x)=>[x,x+1]);
console.log(b);
2,3,7,8,3,4,1,2,0,1
reduce()
procees the array and return single value.
let num=[2,7,3,1,0];
let a=num.reduce(x);
function x(c,d){
T=c+d;
return T;
}
console.log(a);
13 //(2+7=>9+3=>12+1=>13+0=13)
reduceRight()
same as reduce but it is work from (right-to-left) opposite direction.
let num =["h","e","l","l","o"];
let a=num.reduce(sum);
let b=num.reduceRight(sum);
function sum(x,y){
return x+y;
}
console.log(a);
console.log(b);
hello
olleh
every()
it return false ==> condition not satisfied
otherwise,it return true.(like AND operator)
let a=[4,8,11];
let b =a.every((a)=>(a%2==0);
console.log(b);
false
some()
if it is return false ==> no one is sastisfied the condition otherwise,it is return true(like OR operator)
let a=[2,5,7,9,11];
let b=a.some((a)=>(a%2==0));
console.log(b);
true
from()(To be discussed)
create a new copied array from iterable object.
let a="TEASHOP";
let b=Array.from(a);
console.log(b);
"T","E","A","S","H","O","P"
keys()
object type==>return keys
array type==>return values(index value)
const a={
age:35,
id:1258,
height:5.2,
};
const b=Object.keys(a);
console.log(b);
age
id
height
another example
const a=[5,8,9,3];
const b=a.keys();
console.log(...b);
0123
entries()
it's return both index and values of an array
const a=[5,8,9,3];
const b=a.entries();
console.log([...b]);
[0,5]
[1,8]
[2,9]
[3,3]
with()
create a new array by changing/replace one element at a specific index without modifying the original array.
const a=[5,8,"hel",3];
const b=a.with(2,"hello");
console.log(b);
5,8,hello,3
Top comments (0)