Skip to content

Commit 68d2abd

Browse files
committed
derivation of many array methods with examples
1 parent 26faa65 commit 68d2abd

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

arraymethods.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//reduce: accumulate values as specified in function
55

66
// Map :
7-
const arr = [1, 2, 3, 4, 5];
8-
const newarr = arr.map(num => num * 5);
7+
const arr0 = [1, 2, 3, 4, 5];
8+
const newarr = arr0.map(num => num * 5);
99
console.log(newarr);
1010

1111
//filter:
@@ -24,4 +24,62 @@ console.log(reduced);
2424
//find:
2525
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2626
const found = 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+
const arr3 = ['Nick', 'Frank', 'Joe', 'Frank'];
32+
const foundIndex = 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+
const arr4 = ['Nick', 'Frank', 'Joe', 'Frank'];
38+
const foundIndex1 = 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.
45+
46+
let arr5 = [1, 2, 3, 4];
47+
const pushed = arr5.push(5);
48+
console.log(arr5);// [1, 2, 3, 4, 5]console.log(pushed);// 5
49+
50+
//pop: This removes the last item from an array. Again, it modifies the array in place. The function itself returns the item removed from the array.
51+
let arr6 = [1, 2, 3, 4];
52+
const popped = arr6.pop();
53+
console.log(arr6);// [1, 2, 3]console.log(popped);// 4
54+
55+
//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.
56+
57+
let arr7 = [1, 2, 3, 4];
58+
const shifted = arr7.shift();
59+
console.log(arr7);// [2, 3, 4]console.log(shifted);// 1
60+
61+
//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.
62+
63+
let arr8 = [1, 2, 3, 4];
64+
const unshifted = arr8.unshift(5, 6, 7);
65+
console.log(arr8);// [5, 6, 7, 1, 2, 3, 4]console.log(unshifted);// 7
66+
67+
68+
//**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+
let arr9 = ['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.
75+
let arr10 = ['a', 'b', 'c', 'd', 'e'];
76+
const sliced = arr10.slice(2, 4);
77+
console.log(sliced);// ['c', 'd']console.log(arr);// ['a', 'b', 'c', 'd', 'e']
78+
79+
//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.
80+
81+
82+
let arr11 = [1, 7, 3, -1, 5, 7, 2];
83+
const sorter = (firstEl, secondEl) => firstEl - secondEl;
84+
arr11.sort(sorter);
85+
console.log(arr11);

0 commit comments

Comments
 (0)