-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollection.js
More file actions
61 lines (40 loc) · 1.1 KB
/
Copy pathcollection.js
File metadata and controls
61 lines (40 loc) · 1.1 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
// array
let arr = [];
console.log(typeof arr);
let detailsArr = [1, 'arvind', 29];
let numArr = [1, 2, 3, 4, 5];
for (let i = 0; i < numArr.length; i++) {
console.log(`arr index ${i} and value ${numArr[i]}`);
}
// console.log('i val', i); // error bcoz i is let
numArr.forEach((val, index) => {
console.log(`arr index ${index} and value ${val}`);
});
numArr.forEach(val => {
console.log(`value ${val}`);
});
// get all odd nums from arr
let oddArr = [];
numArr.forEach(val => {
if (val % 2 !== 0)
oddArr.push(val);
});
console.log(oddArr);
// array.map -> returns a new array with same num of elems
arr = [1, 2, 3];
let twoTimesArr = arr.map(value => value * 2);
console.log(twoTimesArr)
// arr.filter -> can return less no of elems
// let oddNumArr = arr.filter((value) => {
// if (value % 2 === 0) {
// return false;
// } else
// return true;
// });
let oddNumArr = arr.filter(value => value % 2);
console.log(oddNumArr);
let greaterThan2 = arr.filter(value => value > 2);
console.log(greaterThan2);
console.log(arr.includes(100));
console.log(arr.indexOf(100));
// book examples