|
| 1 | + |
| 2 | +const arr = [1, 2, 3, 4, 5]; |
| 3 | + |
| 4 | +function double(x){ |
| 5 | + return x * 2; |
| 6 | +} |
| 7 | + |
| 8 | +function triple(x){ |
| 9 | + return x * 3; |
| 10 | +} |
| 11 | + |
| 12 | +const outputDouble = arr.map(double); |
| 13 | +console.log(outputDouble); // [2, 4, 6, 8, 10] |
| 14 | +// alernate way to use map |
| 15 | + |
| 16 | +const outputDoubleAlt = arr.map(function(x) { // Using a regular function |
| 17 | + return x * 2; |
| 18 | +}); |
| 19 | +// This is equivalent to the previous example |
| 20 | +// It uses a regular function instead of an arrow function |
| 21 | + |
| 22 | +console.log(outputDoubleAlt); // [2, 4, 6, 8, 10] |
| 23 | + |
| 24 | +const outputDoubleAltArrow = arr.map(x => x * 2); |
| 25 | +console.log(outputDoubleAltArrow); // [2, 4, 6, 8, 10] |
| 26 | + |
| 27 | +const outputTriple = arr.map(triple); |
| 28 | +console.log(outputTriple); // [3, 6, 9, 12, 15] |
| 29 | + |
| 30 | +function binary(x) { |
| 31 | + return x.toString(2); |
| 32 | +} |
| 33 | +const binaryArr = arr.map(binary); |
| 34 | + |
| 35 | +// The above code can be rewritten as : |
| 36 | +const binaryArr1 = arr.map(function binary(x) { |
| 37 | + return x.toString(2); |
| 38 | +}); |
| 39 | + |
| 40 | +// OR -> Arrow function |
| 41 | +const binaryArr2 = arr.map((x) => x.toString(2)); |
| 42 | + |
| 43 | +const users = [ |
| 44 | + { firstName: "Aditya", lastName: "Kumar", age: 23 }, |
| 45 | + { firstName: "Ashish", lastName: "Kumar", age: 29 }, |
| 46 | + { firstName: "Ankit", lastName: "Roy", age: 29 }, |
| 47 | + { firstName: "Pranav", lastName: "Mukherjee", age: 50 }, |
| 48 | +]; |
| 49 | +const fullNameArray = users.map(x => { |
| 50 | + return x.firstName + " " + x.lastName; |
| 51 | +}) |
| 52 | +console.log(fullNameArray); |
| 53 | + |
| 54 | +// Polifill for map |
| 55 | +Array.prototype.myMap = function(callback) { |
| 56 | + let result = []; |
| 57 | + for (let i = 0; i < this.length; i++) { |
| 58 | + result.push(callback(this[i], i, this)); // Pass current element, index, and array to the callback |
| 59 | + } |
| 60 | + return result; |
| 61 | +}; |
| 62 | +// This is a custom implementation of the map function that mimics the behavior of Array.prototype.map |
| 63 | +// It iterates over the array, applies the callback function to each element, and returns a new array with the results. |
| 64 | +// The callback function can take three arguments: the current element, its index, and the array |
| 65 | +// This allows for more flexibility in how the callback is used, similar to the built-in map |
| 66 | +// This custom implementation can be used in the same way as the built-in map function, allowing |
| 67 | +// developers to use it in their code without relying on the built-in method. |
| 68 | +// This is useful for environments where the built-in map function is not available or for educational purposes |
| 69 | +// Usage: |
| 70 | +console.log(arr.myMap(x => x * 200)); // [2, 4, 6] |
| 71 | + |
| 72 | + |
| 73 | + |
0 commit comments