-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraymethods.js
More file actions
52 lines (48 loc) · 1.52 KB
/
Arraymethods.js
File metadata and controls
52 lines (48 loc) · 1.52 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
// for each
let arr = [1, 2, 3, 4, 5];
let print = function (element) {
console.log(element);
};
arr.forEach(print);
// map & filter
let num = [1, 2, 3, 4, 5];
let double = num.map(function (element) {
return element * 2;
});
console.log(double);
// filter-based on condition give a new array if the condition is true it will be included otherwise it will be excluded
let even = num.filter(function (element) {
return element % 2 == 0;
});
console.log(even);
// Every method - return true if every element of array gives true for some functions. Else returns false.(similar to and operator)
let allEven = num.every(function (element) {
return element % 2 == 0;
});
console.log(allEven);
// Some method - return true if any element of array gives true for some functions. Else returns false.(similar to or operator)
let anyEven = num.some(function (element) {
return element % 2 == 0;
})
console.log(anyEven);
// reduce method- it reduces the array to a single value.
let sum = num.reduce(function (acc, element) {
return acc + element;
}, 0);
console.log(sum);
// finding the maximum number in an array
let max = num.reduce(function (acc, element) {
return Math.max(acc, element);
}, 0);
console.log(max);
// check i all numbers in our array are multiple of 10 or not
let arr1 = [10, 20, 30, 40, 50];
let check = arr1.every(function (element) {
return element % 10 == 0;
});
console.log(check);
// default parameter- giving a default value to the arg
function sum1(a, b = 1) {
return a + b;
}
console.log(sum1(10));