-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18. Map.js
More file actions
44 lines (36 loc) · 779 Bytes
/
18. Map.js
File metadata and controls
44 lines (36 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//MAP= same operation on each array
// Example 1
let arr=[1,12,18,24,11];
let newarr=[]
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
newArr.push(element**2)
newArr.push(element+1)
newArr.push(element+1)
}
console.log(newarr)
console.log(arr)
let map= arr.map((e)=>{
return e**2
})
console.log(map)
// Example 2
let arr=[1,2,3,4];
let newarray=[];
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
newarray.push(element**2)
}
console.log(arr)
console.log(newarray)
let newArr= arr.map((e)=>{
return e**2
})
console.log(arr)
console.log(newArr)
// Example 3
let arr=[1,2,3,4]
let newArr= arr.map((e,index,Array)=>{ //can choose anyone
return e**2
})
console.log(newArr)