You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: arraymethods.js
+61-3Lines changed: 61 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@
4
4
//reduce: accumulate values as specified in function
5
5
6
6
// Map :
7
-
constarr=[1,2,3,4,5];
8
-
constnewarr=arr.map(num=>num*5);
7
+
constarr0=[1,2,3,4,5];
8
+
constnewarr=arr0.map(num=>num*5);
9
9
console.log(newarr);
10
10
11
11
//filter:
@@ -24,4 +24,62 @@ console.log(reduced);
24
24
//find:
25
25
constarr2=[1,2,3,4,5,6,7,8,9,10];
26
26
constfound=arr2.find(n=>n>5);
27
-
console.log(found);//It gives 6 only because it breakes when it finds it's match
27
+
console.log(found);//It gives 6 only because it breakes when it finds it's match
28
+
29
+
//findIndex: This works almost identically to find, but rather than returning the first matching element it returns the index of the first matching element. Take the following example, which uses names instead of numbers for clarity.
30
+
31
+
constarr3=['Nick','Frank','Joe','Frank'];
32
+
constfoundIndex=arr3.findIndex(el=>el==='Frank');
33
+
console.log(foundIndex);// 1
34
+
35
+
//indexOf: Works almost identically to findIndex, but instead of taking a function as an argument it takes a simple value. You can use this when you have simpler logic and don’t need to use a function to check whether there is a match.
36
+
37
+
constarr4=['Nick','Frank','Joe','Frank'];
38
+
constfoundIndex1=arr4.indexOf('Frank');
39
+
console.log(foundIndex1);// 1
40
+
41
+
42
+
//*push, pop, shift, unshift**There are a lot of great array method to help add or remove elements from arrays in a targeted fashion.
43
+
44
+
//push: This is a relatively simple method that adds an item to the end of an array. It modifies the array in-place and the function itself returns the item added to the array.
//shift: This removes the first item from an array. Again, it modifies the array in place. The function itself returns the item removed from the array.
//unshift: This adds one or more elements to the beginning of an array. Again, it modifies the array in place. Unlike a lot of the other methods, the function itself returns the new length of the array.
//**splice, slice**These methods either modify or return subsets of arrays.
69
+
//splice: Change the contents of an array by removing or replacing existing elements and/or adding new elements. This method modifies the array in place.
70
+
//The following code sample can be read as: at position 1 of the array, remove 0 elements and insert b.
71
+
letarr9=['a','c','d','e'];
72
+
arr9.splice(1,0,'b');
73
+
74
+
//slice: returns a shallow copy of an array from a specified start position and before a specified end position. If no end position is specified, the rest of the array is returned. Importantly, this method does not modify the array in place but rather returns the desired subset.
//sort: sorts an array based on the provided function which takes a first element and second element argument. Modifies the array in place. If the function returns negative or 0, the order remains unchanged. If positive, the element order is switched.
0 commit comments