-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray-methods.js
More file actions
104 lines (78 loc) · 2.8 KB
/
array-methods.js
File metadata and controls
104 lines (78 loc) · 2.8 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const arr = [1, 2, 3, 4, 5, 6];
//From this array make a new array that is a square of each number
//[1,4,9,16,25,36];
//When You want to perform operation on each and every element of array
//.map goes to each and every elment of the array and performs an operation
//Also, it returns a new array
const squareArray = arr.map(function (element, index) {
//You need to know which element are you currently traversing on
//First Argument is the element that you are traversing upon
//Second argument is the index of the element you are traversing on
//You have to necessarily return the result
return element * element;
});
console.log("Two arrays are", arr, squareArray);
//Add 2 to every number in original Array
//Given an array, return all even Numbers From it
//The condition decides if the element should be present in the new array
//Filter Goes each and every element of the array and makes a new array based on condition
const evenArray = arr.filter(function (element, index) {
//If it return true then element will be present in the array else not
return element % 2 === 0;
});
console.log("Even array is", evenArray);
//In Map length of new array will be definitly equal to length of original array.
//In Filter length of new array might be different from length of original array.
//Given an array you need to make a new array only of numbers
const arr2 = ["Vanshu", 2, 3, 4, 5, "Rajat"];
const numberArray = arr2.filter((element) => {
return typeof element === "number";
});
console.log("Number array is", numberArray);
const arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const firstFiveNumbers = arr3.filter(function (element, index) {
return index < 5;
});
const squared = firstFiveNumbers.map(function (element, index) {
return element * element;
});
console.log("Squared", squared, firstFiveNumbers);
//You have an array of 20 numbers. You owant to make a new array that has square of
//first ten numbers
//Reduce
//Accumulator of all numbers in Array
let nums = [11, 2, 3, 4];
// let Accumulator=0;
// for(let i=0;i<nums.length;i++){
// Accumulator+=nums[i];
// }
//Reduce returns single element out of all the array elements
const sumOfNumbers = nums.reduce(function (accumulator, currentValue, index) {
return accumulator + currentValue;
}, 0);
const arrayOfObjects = [
{
name: "Swagatm",
assignments: 5,
batch:60
},
{
name: "Shekhar",
assignments: 10,
},
{
name: "Vanshu",
assignments: 100,
},
{
name:"Vanshu",
assignments:1000
}
];
//Find the Object in the array whewe Name is Vanshu
//Filter tries to find all the entries that match the criteria
//Find returns the first entry that matches the criteria
const myEntry=arrayOfObjects.findIndex(function(currentObject){
return currentObject.name==="Vanshus"
})
console.log("My Entry is",myEntry);