-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraymethods.js
More file actions
89 lines (55 loc) · 1.84 KB
/
arraymethods.js
File metadata and controls
89 lines (55 loc) · 1.84 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
let arr=["one",2,"three",4,"five"]// index starts from 0
console.log(arr[4]);
arr[3]="four";
console.log(arr);
let fruit=["apple","orange","banana","kiwi","guva"]
fruit.push("grape"); // inserrt elemet last
console.log(fruit);
fruit.pop(); // remove
console.log(fruit);
fruit.shift(); // inserrt elemet first
console.log(fruit);
fruit.unshift("plum"); // inserrt elemet
console.log(fruit);
// slice
console.log(fruit.slice(2,4));// slice add next value index
console.log(fruit.slice(-3,-1)); // negative
//splice
fruit.splice(2,0,"cherry"); // add
console.log(fruit);
fruit.splice(3,1,"tomato"); // update
console.log(fruit);
fruit.splice(4,1,); // remove
console.log(fruit);
// sort reverse
//let fruit=["apple","orange","banana","kiwi","guva"]
console.log(fruit.reverse());// reverse order
console.log(fruit.sort()); //alphabetical order
console.log(fruit.reverse());
//concat index
let fruits=["apple","pomo","banana"]
let one =[1,1,2,3,4,5,1,2]
console.log(one.indexOf(1)); // 1 start index 0 1 end index 6
console.log(one.lastIndexOf(1));
console.log(fruits.concat(one));// combine 2 string
// array method
// map method first get the value check the condition and store it in new array
let num=[1,2,3,4,5,6,7,8,9,10] // iterated through each value
let mul=num.map((el)=>el*5)
console.log(mul);
// filter method store new value in new array based on filter of condition
let num1=[1,2,3,4,5,6,7,8,9,10]
let even=num1.filter((el)=>el%2 ===0)
console.log(even);
// find the largest value in array
let num3=[1,2,4,5,78,97]
for(let i=0;i<num3.length;i++)
{
}
let num2=[1,2,3,4,7,10,25,32,56,78,12,75,45]
let divisablebytwoandfive=num2.filter((el)=> el%2===0 &&el%5===0)
console.log(divisablebytwoandfive);
// reduce method it give single output
let sum=num.reduce((acc,el)=>acc+el,10) // 10 save in accumulator
console.log(sum);
//