DEV Community

SIYAMALA G
SIYAMALA G

Posted on

Array Iterations

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);
Enter fullscreen mode Exit fullscreen mode
watermelon
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
2,3,7,8,3,4,1,2,0,1
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
13  //(2+7=>9+3=>12+1=>13+0=13)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
hello
olleh
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
true
Enter fullscreen mode Exit fullscreen mode

from()(To be discussed)
create a new copied array from iterable object.

let a="TEASHOP";
let b=Array.from(a);
console.log(b);
Enter fullscreen mode Exit fullscreen mode
"T","E","A","S","H","O","P"
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
age
id
height
Enter fullscreen mode Exit fullscreen mode

another example

const a=[5,8,9,3];
    const b=a.keys();
    console.log(...b);
Enter fullscreen mode Exit fullscreen mode
0123
Enter fullscreen mode Exit fullscreen mode

entries()
it's return both index and values of an array

const a=[5,8,9,3];
const b=a.entries();
console.log([...b]);
Enter fullscreen mode Exit fullscreen mode
[0,5]
[1,8]
[2,9]
[3,3]
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
5,8,hello,3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)